gpt4 book ai didi

c++ - 适当的地方初始化静态成员变量的值

转载 作者:行者123 更新时间:2023-11-28 07:20:10 25 4
gpt4 key购买 nike

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

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

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

CFoo::~CFoo()
{
}

谢谢

最佳答案

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

CFoo.cpp

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

CFoo::CFoo()

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

CFoo::~CFoo()
{
}

关于c++ - 适当的地方初始化静态成员变量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19614205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com