0

在构造函数类中初始化我的静态成员变量是否合适?

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

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

CFoo::~CFoo()
{
}

谢谢

4

1 回答 1

1

你还没有define静态成员。您需要在 CFoo.cpp 中定义它。

CFoo.cpp

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

CFoo::CFoo()

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

CFoo::~CFoo()
{
}
于 2013-10-27T03:11:38.047 回答