***靜態成員的定義及初始化 for c++ for新用法
阿新 • • 發佈:2018-06-23
用法 靜態數組 In const AI code 引用 clu RR
靜態成員的初始化要在類外不然報錯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 voidget(){ cout<<"靜態成員e的值為"<<e ++<<endl; cout<<"靜態數組內容如下:"<<endl; for(auto k : d){///如果要修改值 需要引用 for(int &k : d) k ++; cout<<k; } cout<<endl; } private: //static int f = 1;///錯誤 conststatic 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新用法