2013-10-27 73 views
0

在構造函數類中初始化我的靜態成員變量是否合適?適當的位置來初始化靜態成員變量的值

// CFoo.h 
class CFoo 
{ 
public: 
    CFoo(); 
    ~CFoo(); 
    static std::string str; 
}; 

// CFoo.cpp 
CFoo::CFoo() 
{ 
    str = "HELLO"; 
} 

CFoo::~CFoo() 
{ 
} 

感謝

+0

http://stackoverflow.com/questions/185844/initializing-private-static-members –

回答

1

你沒有define靜態成員呢。你需要在CFoo.cpp中定義它。

CFoo.cpp

std::string CFoo::str; // define str 

CFoo::CFoo() 

{ 
    str = "HELLO"; // reset str is fine 
} 

CFoo::~CFoo() 
{ 
}