• 【C++】拷贝构造函数和赋值符函数


    在C++中,调用拷贝构造函数有三种情况:

    1.一个对象作为函数参数,以值传递的方式传入函数体

    2.一个对象作为函数返回值,以值传递的方式从函数返回

    3.一个对象用于给另外一个对象进行初始化(复制初始化).

    拷贝构造函数必须以引用的形式传递(参数为引用值).其原因如下:

    当一个对象以传递值的方式传一个函数的时候,拷贝构造函数自动的调用来生成函数中的对象.

    这样会导致无限循环地调用拷贝构造函数,直至栈溢出.

    以前,一直有个误解,以为以同类型的对象调用"="时,就会调用赋值符.参看以下的例子:

    复制代码
     1 class CTest {
     2 public:
     3     CTest();
     4     CTest(const CTest&);
     5     CTest& operator=(const CTest &);
     6 };
     7 CTest::CTest()
     8 {
     9     cout<<"Constructor of CTest"<<endl;
    10 }
    11 CTest::CTest(const CTest& arg)
    12 {
    13     cout<<"Copy Constructor of CTest"<<endl;
    14 }
    15 CTest& CTest::operator=(const CTest& arg)
    16 {
    17     cout<<"Assign function of CTest"<<endl;
    18 }
    19 int main()
    20 {
    21     CTest a;
    22     CTest b(a);
    23     CTest c = a;
    24     a = c;
    25     return 0;
    26 }
    复制代码

    按照以前的理解,第21~24行代码,应该分别调用构造函数,拷贝构造函数,赋值符函数,赋值符函数.

    然而最终如下,不是如自己所想...说明以前的理解是错误的.

    Constructor of CTest
    Copy Constructor of CTest
    Copy Constructor of CTest
    Assign function of CTest

    第23行代码调用的是拷贝构造函数,不是赋值符函数,但第24行代码调用的赋值符函数,不是拷贝构造函数.原因如下:

    拷贝构造函数创建新的对象,而赋值符函数不创建新对象,它要求"="的左右对象均已存在,它的作用就是把"="右边的对象的值赋给左边的对象.

    虽然编译器会提供拷贝构造函数和赋值符函数,但是有时候编译器提供的这些函数,并不能满足我们的需求,因而需要自定义拷贝构造函数和赋值函数.

    这里就会引出一个新问题,什么时候需要自定义拷贝构造函数和赋值符函数.

    简单的规则:如果需要定义一个非空的析构函数,那么,通常情况下也需要定义一个拷贝构造函数和赋值符函数.

    通常的原则是:

    1.对于凡是包含动态分配成员或包含指针成员的类都应该提供拷贝构造函数;

    2.在提供拷贝构造函数的同时,还应该考虑重载"="赋值操作符,即提供赋值符函数.

    当我们知道需要自定义拷贝构造函数和赋值符函数时,就得考虑如何良好的实现它们.

    当自定义copying函数(包含拷贝构造函数和赋值符函数)时,需要确保以下两点:

    1.复制所有的local成员变量

    2.调用所有base classes内的适当的copying函数,完成基类的copying.

    下面是一个具体的例子:

    复制代码
     1 void logcall(const std::string& funcName);    //制造一个log entry
     2 class Customer {
     3 public:
     4     ...
     5     Customer(const Customer& rhs);
     6     Customer& operator=(const Customer& rhs);
     7     ...
     8 private:
     9     std::string name;
    10 };
    11 Customer::Customer(const Customer& rhs):name(rhs.name)
    12 {
    13     logCall("Customer copy constructor");
    14 }
    15 Customer& Customer::operator=(const Customer& rhs)
    16 {
    17     logCall("Customer copy assignment operator");
    18     name = rhs.name;                    //疑惑,为什么在copying函数里可以通过对象调用私有变量?
    19     return *this;
    20 }
    21 
    22 class PriorityCustomer:public Customer {
    23 public:    
    24     ...
    25     PriorityCustomer(const PriorityCustomer& rhs);
    26     PriorityCustomer& operator=(const PriorityCustomer& rhs);
    27     ...
    28 private:
    29     int priority;
    30 };
    31 PriorityCustomer::PriorityCustomer(const PriorityCustomer& rhs):Customer(rhs),priority(rhs.priority)
    32 {
    33     logCall("PriorityCustomer copy constructor");
    34 }
    35 PriorityCustomer& PriorityCustomer::operator=(const PriorityCustomer& rhs)
    36 {
    37     logCall("PriorityCustomer copy assignment operator");
    38     Customer::operator=(rhs);                //对base class成分进行赋值动作
    39     priority = rhs.priority;
    40     return *this;
    41 }
    复制代码

    第18行代码中,通过对象调用私有变量,似乎违背了私有变量的含义,有点无法理解,具体的分析和理解,请参考:

    http://www.cnblogs.com/dwdxdy/archive/2012/07/17/2595741.html

    使用opertator=函数给对象赋值时,若右边的对象和调用对象相同,即自我赋值,会引发自我赋值的不安全问题.具体分析和解决方案,请参考:

    http://www.cnblogs.com/dwdxdy/archive/2012/07/17/2595821.html

    参考资料:http://baike.baidu.com/view/1266959.htm

  • 相关阅读:
    python pytesseract模块,报错
    CSS清除浮动_清除float浮动
    CSS中@import与link的具体区别
    js substr和substring的区别
    WebStorm 自定义字体+颜色+语法高亮+导入导出用户设置
    Jquery中的重置
    table标签中thead、tbody、tfoot的作用
    下拉列表框的几个属性
    使用GDI绘制文本
    使用GDI绘制一条直线
  • 原文地址:https://www.cnblogs.com/wanqieddy/p/3359541.html
Copyright © 2020-2023  润新知