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

编程入门 行业动态 更新时间:2024-10-24 11:15:13
本文介绍了参数化构造函数不是复制一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

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

本文发布于:2023-11-25 12:03:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629669.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   参数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!