gpt4 book ai didi

c++ - 什么时候调用全局对象的构造函数?

转载 作者:搜寻专家 更新时间:2023-10-31 01:24:40 25 4
gpt4 key购买 nike

<分区>

这是我写的一些代码:

#include <iostream>

class Number
{
int n;
public:
Number(int n)
{
std::cout << "Constructed object: " << n << std::endl;
this->n = n;
}
};

Number b = 2; //when does this ctor get called?

int main()
{
Number a = 4;
}

输出:

Constructed object: 2
Constructed object: 4

什么时候调用全局对象的构造函数?就在 main 执行之前?

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