为什么不能通过直接初始化语法来初始化类数据成员?
我很好奇为什么类数据成员不能使用 ()
语法初始化?考虑下面的例子:
#include <iostream>
class test
{
public:
void fun()
{
int a(3);
std::cout<<a<<'\n';
}
private:
int s(3); // Compiler error why???
};
int main()
{
test t;
t.fun();
return 0;
}
程序编译失败并且失败。给出以下错误”
11 9 [错误] 数字常量之前的预期标识符
11 9 [错误] 数字常量前应有“,”或“...”
为什么? C++ 标准对类数据成员的初始化有何规定?
I am curious to know why class data members can't be initialized using the ()
syntax? Consider the following example:
#include <iostream>
class test
{
public:
void fun()
{
int a(3);
std::cout<<a<<'\n';
}
private:
int s(3); // Compiler error why???
};
int main()
{
test t;
t.fun();
return 0;
}
The program fails in compilation & gives the following errors"
11 9 [Error] expected identifier before numeric constant
11 9 [Error] expected ',' or '...' before numeric constant
Why? What does the C++ standard say about initialization of class data members?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
导致该功能推出的早期提案解释说这是为了避免解析问题。
以下只是其中提供的示例之一:
Early proposals leading to the feature's introduction explain that this is to avoid parsing problems.
Here's just one of the examples presented therein: