gpt4 book ai didi

c++ - 参数化构造函数不是拷贝

转载 作者:太空宇宙 更新时间:2023-11-03 10:32:17 25 4
gpt4 key购买 nike

假设我们有一个类 A 包含作为同一类的成员:

Class A{
const A &a;
}

我想创建一个传递该成员值的参数化构造函数,但我不想定义该类的复制构造函数。

A(const A& memberA): a(memberA)     {}

怎么可能指示编译器这样的东西?

谢谢

最佳答案

构造函数只能引用它构造的类复制构造函数,无论您是否希望它是一个。复制构造函数是这样定义的:

A non-template constructor for class X is a copy constructor if its first parameter is of type X&, const X&, volatile X& or const volatile X&, and either there are no other parameters or else all other parameters have default arguments.

您可以将其声明为explicit 以限制类的复制方式(例如防止A a = A()),但它仍然是一个复制构造函数因为它有那个签名。

关于c++ - 参数化构造函数不是拷贝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13413419/

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