gpt4 book ai didi

c++ - 为什么这段代码可以省略一个拷贝?

转载 作者:太空狗 更新时间:2023-10-29 19:38:51 24 4
gpt4 key购买 nike

<分区>

Possible Duplicates:
constructor invocation mechanism
Why is it an error to use an empty set of brackets to call a constructor with no arguments?

为什么可以这样code删除 A 的所有拷贝?

#include <iostream>

class A
{
public:
A() {}
A(const A&) { std::cout << "Copy" << std::endl; }
};

class B
{
public:
B(const A& a_) : a(a_) {}
private:
A a;
};

int main()
{
B b(A());
}

此代码显然没有复制 A,并且在 ideone 的 gcc 3.4 下不输出任何内容。

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