为什么复制构造函数不是通过引用而是通过引用传递的 [英] Why copy constructor is passed by reference not by pointer

查看:120
本文介绍了为什么复制构造函数不是通过引用而是通过引用传递的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello everyone,



我对复制构造函数有疑问。



为什么复制构造函数是通过引用而不是通过指针传递的?







我很清楚为什么它不能通过价值传递。



请帮忙。





问候,

Joy

Hello Everyone,

I have a question on copy constructor.

Why copy constructor is passed by reference not by pointer?



I am clear as to why it can not be passed by value.

Please help.


Regards,
Joy

推荐答案

解决方案1和2中已经揭示了一些事实(请忽略3),但是其中一个原因是,这允许您将可以转换或评估的内容传递给您的类类型,例如表达式,而无需自己提供临时变量。



Some of the truth has been revealed in Solutions 1 and 2 (please ignore 3), but one of the reasons is that this allows you to pass things that can be converted or evaluated to your class type, such as expressions, without having to provide a temporary variable yourself.

class cVec {
public:
   cVec(const cVec& other);
   cVec(double x, double y);
   cVec operator+(const cVec& first, const cVec& second);
private:
   double x_;
   double y_;
};

void foo() {
   cVec a(3.14, 2.71);
   cVec b(a + a); // now try and pass that by pointer!
}


由于复制构造函数是由编译器在未由用户指定时生成的,因此对于用于编译器定义的复制构造函数的用户定义的复制构造函数使用相同的签名是有意义的。 。



如果有人定义了一个带指针而不是引用的构造函数,那么如果调用者忘记取新对象所针对的对象的地址创建,然后使用带指针的user-constructor,而不是使用编译器生成的复制构造函数(wioth参考)。



使用它也是有意义的一致性的参考。例如,所有运算符如=,==,< =,+ =通常都会使用引用来定义。



请参考以下链接: -

深入了解 C ++ Copy Constructor [ ^ ]
Since the copy constructor is generated by the compiler when not specified by the user, it make sense to use the same signature for user-defined copy constructor that is used for compiler-defined copy constructor.

If someone define a constructor that take a pointer instead of a reference, then if the caller forget to take the address of the object from which the new object is to be created, then instead of using user-constructor with a pointer, the compiler generated copy constructor (wioth a reference) would be used.

It also make sense to use a reference for consistency. For exemple, all operators like =, ==, <=, += would normally be defined using references.

Refer below link:-
C++ Copy Constructor in depth [^]




首先尝试理解什么是引用以及何时需要它。

引用是作为另一个变量的别名的变量。引用是隐式的const所以它们必须在声明时给出一个值。



首先,Const引用通常用作函数参数,因为const引用允许我们访问但不能更改对象的值,它们可以用来给函数访问一个对象,但是保证调用者该函数根本不会改变该对象。



其次,参考ce就像一个隐式解除引用的const指针。

因为引用总是指向有效对象,并且永远不能指向释放的内存,所以引用比指针更安全。因此,通常应该优选参考。指针通常只应用于引用不足的情况。因此,防止内存损坏问题引用优先于指针。



考虑到这两个因素,我们可以给出复制构造函数,如:


first try to understand what is a reference and when is it needed .
References are variables that act as an alias to another variable.References are implicitly const so they must be given a value upon declaration.

First, Const references are often used as function parameters because const references allow us to access but not change the value of an object, they can be used to give a function access to an object, but give assurance to caller that the object will not be changed at all by the function.

Secondly, a reference acts like a const pointer that is implicitly dereferenced.
Because references always "point" to valid objects, and can never be pointed to deallocated memory, references are safer to use than pointers. Thus the reference should generally be preferred. Pointers should generally only be used in situations where references are not sufficient .So, to prevent memory corruption issues References are preferred to pointers.

Taking both these factors we can give a copy-constructor like :
// Copy constructor
    MyClass(const MyClass &cSource)
    {
        m_nVariable1 = cSource.m_nVariable1;
    }



因此传递的源对象永远不会在复制构造函数中被修改,仅用于只读目的。


Thus the source object being passed is never modified within the copy-constructor and is used solely for read-only purposes.


这篇关于为什么复制构造函数不是通过引用而是通过引用传递的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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