【问题标题】:forbids in-class initialization of non-const static member禁止非常量静态成员的类内初始化
【发布时间】:2013-02-13 21:53:02
【问题描述】:

我是 Java 开发人员,我是 C++ 新手。试试下面的代码,它会显示非常量静态成员的初始化错误。

class omtFirst {
int a = 8,b = 2;
public:
void printThis(string);
int addNumber(){        
    return (a+b);
};
};

在网上搜索后,我发现我们无法在类描述中初始化变量。但在 Java 中这是可能的。为什么在 C++ 中我们不能在类描述中初始化变量。

【问题讨论】:

    标签: c++


    【解决方案1】:

    因为标准是这样说的,编译器有义务效仿。没有技术原因;实际上在 C++11 中是 possible 这样做。

    【讨论】:

      猜你喜欢
      • 2013-12-17
      • 1970-01-01
      • 2011-10-06
      • 2012-02-18
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多