标签: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(); }
标签:static names 静态成员 namespace public color code clu end
原文地址:https://www.cnblogs.com/8023spz/p/9217080.html