码迷,mamicode.com
首页 > 编程语言 > 详细

***静态成员的定义及初始化 for c++ for新用法

时间:2018-06-23 15:46:09      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:static   names   静态成员   namespace   public   color   code   clu   end   

静态成员的初始化要在类外不然报错error: ISO C++ forbids in-class initialization of non-const static member ‘***‘

但是声明为const的变量就可以了,即使是static的

#include <iostream>
#include <cstdlib>
using namespace std;
class spz
{
public:
    spz(){
        cout<<"构造调用"<<endl;
    }
    static void get(){
        cout<<"静态成员e的值为"<<e ++<<endl;
        cout<<"静态数组内容如下:"<<endl;
        for(auto k : d){///如果要修改值 需要引用 for(int &k : d)
            k ++;
            cout<<k;
        }
        cout<<endl;
    }
private:
    //static int f = 1;///错误
    const static int g = 1;///正确
    static int d[8];
    static int e;
};
int spz::d[8] = {1,2};///初始化
int spz::e = 12;
int main() {
    spz::get();
    spz::get();
}

 

***静态成员的定义及初始化 for c++ for新用法

标签:static   names   静态成员   namespace   public   color   code   clu   end   

原文地址:https://www.cnblogs.com/8023spz/p/9217080.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!