gpt4 book ai didi

c++ - 静态对象初始值设定项 C++

转载 作者:太空狗 更新时间:2023-10-29 21:47:34 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
file scope and static floats
What are static variables?

这是一本书中的代码。

class X
{
int i;
public:
X(int ii = 0) : i(ii) {cout<<i<<endl;} // Default
~X() { cout << "X::~X()" << endl; }
};
void f()
{
static X x1(47);
static X x2; // Default constructor required
}

int main()
{
f();

return 0;
}

我的问题是为什么我要像在函数 f() 中一样将对象声明为静态对象?如果我不将 x1 和 x2 声明为静态会怎样?

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