2011年4月13日 星期三

在class 中 定義字串常數

我們都知道 在class 定義中
only static const int type 可以被定義

但是今天如果想定義一個字串 在class中被用到
要如何做呢

http://stackoverflow.com/questions/1563897/c-static-constant-string-class-member


You have to define your static member outside the class definition and provide the initailizer there.
// In a header file (if it is in a header file in your case)
class A {   private:      
  static const string RECTANGLE;
};
// In one of the implementation files
const string A::RECTANGLE = "rectangle";
The syntax you were originally trying to use (initializer inside class definition) is only allowed with integral and enum types.

沒有留言: