warning: in-class initialization of non-static data member is a C++11 extension [-Wc++11-exten...

C++11结构体内成员初始化
本文探讨了C++11中结构体成员变量的内联初始化特性,包括非静态成员、常量成员及静态成员的不同初始化方式,并讨论了编译警告与错误的原因。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值