参数化构造函数不是复制一个

参数化构造函数不是复制一个

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

问题描述

假设我们有一个类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:

你可以声明它 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.

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

08-18 13:58