C++类内定义静态变量只限定与integral类型,比如int、char、long、float、double都行,但是string不行
#include<iostream> #include<string> using namespace std; class testClass { public: static const int _dataI = 2; static const long _dataL = 389l; static const char _dataC = 'A'; }; class testClass2 { public: static int _a; }; int testClass2::_a = 89; class testClass3 { public: static const float _dataF = 2.3; static const double _dataD = 389; // static const string _dataS = "CourageK"; // not integral members }; int main() { cout<<testClass::_dataI<<endl; cout<<testClass::_dataL<<endl; cout<<testClass::_dataC<<endl; cout<<testClass2::_a<<endl; cout<<testClass3::_dataF<<endl; cout<<testClass3::_dataD<<endl; return 0;
}
输出结果为:
2
389
A
89
2.3
389
时间: 2024-10-11 10:29:32