这个问题在这里已经有了答案:
How do C++ class members get initialized if I don't do it explicitly?
(8 个回答)
Value and size of an uninitialized std::string variable in c++
(5 个回答)
1年前关闭。
我有这个字符串 s
和 t
在类。
class A
{
private:
static string s;
string t;
public:
void printString()
{
cout<<s<<t<<endl;
}
};
我尝试打印这些字符串,如果没有初始化,每次它们都是空的。所以我的问题是在构造函数初始化之前类字符串是否总是空的。静态和非静态字符串也是如此吗?
最佳答案
在这两种情况下,都会调用默认的字符串构造函数。 std::string
的默认构造函数类将始终将其初始化为空字符串。
关于c++ - 类字符串是否总是被初始化为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65026407/