复制构造函数参数 [英] copy constructor parameters

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

问题描述

在复制构造函数中,为什么参数需要有与它们相关联的默认值?
如果没有与它们相关联的默认值,并且在构造函数中提供了多个参数,会发生什么?

In a copy constructor why do arguments need to have default values associated with them? What happens if there are no default values associated with them and more than one argument is provided in the constructor?

例如:

X(const X& copy_from_me, int = 10);

具有int的默认值,但是:

has a default value for the int, but this:

X(const X& copy_from_me, int);

在第二种情况下会发生什么?

does not. What happens in this second case?

http:/ /en.wikipedia.org/wiki/Copy_constructor

推荐答案

复制构造函数总是带有一个参数,类型,有可能其他参数,但它们必须具有默认值。

A copy constructor always takes one parameter, reference to the type for which it belongs, there maybe other parameters but they must have default values.

复制构造函数被称为复制函数,复制构造函数的目的是通过使用与类型相同类型的对象来创建类型的对象

An copy constructor is called as an copying function and the purpose of the copy constructor is to create an object of a type by using an object of the same type as basis for creation of the new type.

标准规定复制构造函数的类型为:

The Standard specify's that the copy constructor be of the type:

T(const &T obj);

这基本上允许在通过值调用函数期间创建临时对象或通过值返回类型的对象。

此语法便于创建一个新对象:

This basically allows creation of temporary objects during calling functions by value or returning objects of the type by value.
This syntax facilitates creation of an new object as:

T obj1(obj2);      <--------- Direct Initialization
T obj1 = obj2;     <--------- Copy Initialization

如果将附加参数传递给复制构造函数不会被强制具有默认值,那么使用上述语法的对象构造将是不可能的。

因此,严格条件,

也许其他参数复制到复制构造函数,但它们必须具有默认值。

If the additional arguments being passed to the copy constructor would not be mandated to have default values then the construction of objects using the above syntax would not be possible.
Hence the strict condition,
there maybe other parameters to a copy constructor but they must have default values.

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

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