initialization of non-static data member is a C++11 extension [-Wc++11-extensions]

struct test{
int a=1111111;

}test;

vincentdeMacBook-Air:datasturte vincent$ g++ struct.cpp -o structpp
struct.cpp:4:6: warning: in-class initialization of non-static data member is a
      C++11 extension [-Wc++11-extensions]
int a=11111111;
     ^
1 warning generated.

struct test{
const int a=1111111;

}test;

vincentdeMacBook-Air:datasturte vincent$ g++ struct.cpp -o structpp
struct.cpp:4:12: warning: in-class initialization of non-static data member is a
      C++11 extension [-Wc++11-extensions]
const int a=11111111;
           ^
struct.cpp:29:4: error: cannot assign to non-static data member 'a' with
      const-qualified type 'const int'
q.a=10;
~~~^
struct.cpp:4:11: note: non-static data member 'a' declared const here
const int a=11111111;
~~~~~~~~~~^~~~~~~~~~
1 warning and 1 error generated.
vincentdeMacBook-Air:datasturte vincent$ g++ struct.cpp -o structpp
struct.cpp:29:4: error: cannot assign to static data member 'a' with
      const-qualified type 'const int'
q.a=10;
~~~^

struct test{

static int a=11111111;

}test;

 

struct.cpp:4:18: note: static data member 'a' declared const here
static const int a=11111111;
~~~~~~~~~~~~~~~~~^~~~~~~~~~
1 error generated.


vincentdeMacBook-Air:datasturte vincent$ g++ struct.cpp -o structpp struct.cpp:4:12: error: non-const static data member must be initialized out of line static int a=11111111; ^ ~~~~~~~~ 1 error generated.

  同样问题:http://stackoverflow.com/questions/13662441/c11-allows-in-class-initialization-of-non-static-and-non-const-members-what-c

 

原文链接: https://www.cnblogs.com/vincentqliu/p/6553258.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    initialization of non-static data member is a C++11 extension [-Wc++11-extensions]

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/250879

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月14日 上午4:50
下一篇 2023年2月14日 上午4:51

相关推荐