- //今天在CSDN上看到一份很好的总结,原文如下
-
- #include
"stdafx.h" -
- #include
- #include
-
- using
namespace std; -
- ///
- class
A - {
- public:
-
A() -
{ -
-
} -
- private:
-
int a = 0; // 非静态常成员 - };
-
- int
main() - {
- }
-
- //
错误 只有静态的常成员才可以在头文件中初始化. - //error
C2864: 'A::a' : only static const integral data members can be initialized within a class -
-
- ///
- class
A - {
- public:
-
A() -
{ -
cout<<"a = "<<a<<endl; -
} -
- private:
-
static int a = 0; // 静态但非常成员 - };
-
- int
main() - {
-
A a; -
getch(); - }
-
- //
错误, 只有静态的常量才可以在头文件中初始化. - //error
C2864: 'A::a' : only static const integral data members can be initialized within a class -
-
- ///
- class
A - {
- public:
-
A() -
{ -
a = 0; -
} -
- private:
-
const int a; - };
-
- int
main() - {
- }
-
- //
错误 常成员只能在初始化列表中进行. - //error
C2758: 'A::a' : must be initialized in constructor base/member initializer list -
-
- ///
- class
A - {
- public:
-
A() -
{ -
cout<<"a = "<<a<<endl; -
} -
- private:
-
const int a; // 没有初始化,const 成员使用之前必须在初始化列表中初始化. - };
-
- int
main() - {
-
A a; -
getch(); - }
-
- //error
C2758: 'A::a' : must be initialized in constructor base/member initializer list -
-
- ///
- class
A - {
- public:
-
A() -
: a(1) // 不可以在初始化列表中初始化 -
{ -
-
} -
- private:
-
static int a; - };
-
- int
main() - {
- }
-
- //
错误 静态成员只能在类外进行初始化,这是为什么来着??????忘了... - //error
C2438: 'a' : cannot initialize static class data via constructor -
-
- ///
- class
A - {
- public:
-
A() -
{ -
cout<<"a = "<<a<<endl; -
} -
- private:
-
static int a; // 没有初始化 - };
-
- int
main() - {
-
A a; -
getch(); - }
-
- //
错误,静态成员不会自已初始化,使用之前要初始化.否则会产生连接错误 - //
error LNK2001: unresolved external symbol "private: static int A::a" (?a@A@@0HA)
头文件中的static和const成员的初…
最新推荐文章于 2018-08-16 00:09:04 发布