小编Ern*_*nie的帖子

在类定义之外定义静态const

我们应该static const在类定义之外定义一个成员,即使它是在类中初始化的吗?

#include<iostream>  
using namespace std;  
class abc  
{  
    static const int period=5;  
    int arr[period];  
  public:  
    void display()   
    {   
        cout<<period<<endl;  
    }  
};

const int abc::period;   

int main()   
{   
    abc a;  
    a.display();   
    return 0;  
}
Run Code Online (Sandbox Code Playgroud)

评论之后// const int abc::period;,两个版本的代码在gcc 4.3.4上运行良好.所以我想问为什么两个版本都可以使用,哪个版本符合标准?

c++ c++11

6
推荐指数
1
解决办法
3334
查看次数

标签 统计

c++ ×1

c++11 ×1