【发布时间】:2013-01-07 20:47:21
【问题描述】:
#include <iostream>
using namespace std;
class T1
{
const int t = 100;
public:
T1()
{
cout << "T1 constructor: " << t << endl;
}
};
当我尝试用 100 初始化 const 成员变量 t 时。但它给了我以下错误:
test.cpp:21: error: ISO C++ forbids initialization of member ‘t’
test.cpp:21: error: making ‘t’ static
如何初始化const 值?
【问题讨论】:
-
使用 c++11 可以查看此链接stackoverflow.com/questions/13662441/…
-
c++11 支持在成员声明中包含大括号或等号初始化器