gpt4 book ai didi

c++ - 复制构造函数省略?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:31:14 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Why has the destructor been called only once?

鉴于下面的代码,我无法理解 gcc 中的输出。我希望创建和销毁两个对象,但只看到对构造函数和析构函数的一次调用。这里发生了什么?

#include <string>
#include <iostream>

struct Huge{
Huge() { std::cout << "Constructor" << std::endl; }
Huge(Huge const &r) { std::cout << "Copy Constructor" << std::endl; }
~Huge() { std::cout << "Destructor" << std::endl; }
};

Huge g() {
std::cout << "Entering g" << std::endl;
Huge temp;
std::cout << "Exiting g" << std::endl;
return temp;
}

int main(){
Huge h2(g());
std::cout << "Before leaving main" << std::endl;
}

这段代码在 g++ (4.4) 中的输出是

Entering g

Constructor

Exiting g

Before leaving main

Destructor

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