参数化构造函数不是复制一个 [英] Parametized Constructor Not an Copy one

查看:175
本文介绍了参数化构造函数不是复制一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Say we have a class A that contains as a member of the same class:

Class A{
   const A &a;
}

我想创建一个参数化的构造函数,我不想定义类的复制构造函数。

I want to create a parametized constructor that passed the value of that member, but I do not want to define the copy constructor of the class.

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

如何表示编译器这样的东西?

How could indicate the compiler such thing?

感谢

推荐答案

一个构造函数只需要引用它构造的类一个复制构造函数,无论你想要它是一个或没有。复制构造函数定义如下:

A constructor that can take just a reference to the class it constructs is a copy constructor, whether you want it to be one or not. Copy constructors are defined thus:


class X 的非模板构造函数复制构造函数,如果其第一个参数是 X& const X& volatile X& const volatile X& ,并且没有其他参数或所有其他参数都有默认参数。

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()),但它仍然是一个复制构造函数,只要它有该签名。

You could declare it explicit to restrict how the class can be copied (preventing A a = A() for example), but it's still a copy constructor as long as it has that signature.

这篇关于参数化构造函数不是复制一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆