/home/winxp/project/c++Partices/greeting/greeting.cpp:6:31:warning:</span> non-static data member initializers only available with -std=c++11 or -std=gnu++11
const std::string message = "Hello!";
^
編譯器說無法初始化non-static data member,還說該功能只能在c++11(以上?)使用
non-static不行 那麼 static呢?
將第五行改成這樣:
1
conststaticstd::string message = "Hello!";
嘗試編譯:
1
2
3
4
5
6
/home/winxp/project/c++Partices/greeting/greeting.cpp:6:38: error: invalid in-class initialization of static data member of non-integral type ‘const string {aka const std::__cxx11::basic_string<char>}’
floating point或其他user data type可能就無法直接交給ISA處理了,因此沒辦法直接inline,在runtime需要有一塊記憶體儲存其data member也需要address以供參照。
char 是integral type那麼char[]是嗎?
Nope!
1
2
3
/home/winxp/project/c++Partices/greeting/greeting.cpp:6:33: error: invalid in-class initialization of static data member of non-integral type ‘const char []’
A class is typically declared in a header file and a header file is typically included into many translation units. However, to avoid complicated linker rules, C++ requires that every object has a unique definition. That rule would be broken if C++ allowed in-class definition of entities that needed to be stored in memory as objects.