首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

种类型的const变量必须要定义构造函数吗

2013-10-02 
类类型的const变量必须要定义构造函数吗#includeiostreamusing namespace stdclass B{public:B(){b2}

类类型的const变量必须要定义构造函数吗

#include<iostream>
using namespace std;
class B
{
public:
  B()
  {
    b=2;
  }
  int b;
};
class A
{
public:
  A()
  {}
 int i;
  B a;
};
int main()
{
 
const A aa;
 //using A::B;
 cout<<sizeof(A)<<endl;
 // B b;
}

如果不定义A()
构造函数不行,这个不是编译器自动定义吗?
[解决办法]
主楼的编译错误是 c++ 标准要求的,by c++11 8.5/6
If a program calls for the default initialization of an object of a const-qualified type T, T shall be a class type with a user-provided default constructor.

所以 A 必须提供用户自定义的构造函数,即便该构造函数只具有默认合成的功能。

热点排行