繁体   English   中英

当有复制构造函数时,是否总是需要赋值运算符?

[英]Is assignment operator always necessary when there is a copy constructor?

我正在学习构造函数和析构函数,并且已经学会了三个规则

我现在正在播放tutorialspoint中的一个小例子。 我注意到该示例没有赋值运算符,但代码以某种方式运行良好。 例如,对于Line a(b) ,当我改变a中的内容时,例如*(a.ptr),*(b.ptr)不改变。

我还写了一个赋值运算符(注释),代码也可以。

现在我很困惑。 在某些情况下,似乎只有复制构造函数就足够了。 任何人都可以对此发表评论,以帮助我更好地了解调用复制构造函数所涉及的内存分配机制吗?

谢谢!

#include <iostream>

using namespace std;

class Line
{
   public:
      int getLength( void );
      Line( int len );             // simple constructor
      Line( const Line &obj);  // copy constructor
      ~Line();                     // destructor
      void doubleLength(void);
     Line &operator=(const Line &);

   private:
      int *ptr;
};

// Member functions definitions including constructor
Line::Line(int len)
{
    cout << "Normal constructor allocating ptr" << endl;
    // allocate memory for the pointer;
    ptr = new int; //simply allocates memory for one integer, and returns a pointer to it.
    *ptr = len;
}

Line::Line(const Line &obj)
{
    cout << "Copy constructor allocating ptr." << endl;
    ptr = new int;
   *ptr = *obj.ptr; // copy the value
}

//  // copy assignment operator, added by me
//  Line& Line::operator=(const Line& that)
//  {
//      if (this != &that)
//      {
//          delete ptr;
//          // This is a dangerous point in the flow of execution!
//          // We have temporarily invalidated the class invariants,
//          // and the next statement might throw an exception,
//          // leaving the object in an invalid state :(
//          this->ptr = new int;
//          this->ptr = that.ptr;

//      }
//      return *this;
//  }


Line::~Line(void)
{
    cout <<  "Freeing memory " << ptr << endl;
    delete ptr;
}
int Line::getLength( void )
{
    return *ptr;
}

void Line::doubleLength(void)
{
    *ptr = *ptr * 2;
}

void display(Line obj)
{
   cout << "Length of line : " << obj.getLength() <<endl;
}

// Main function for the program
int main( )
{
   Line line1(10);

//     Line line2 = line1; // This also calls copy constructor
   Line line2(line1);   // performed by copy assignment operator    
   line2.doubleLength();
   display(line1);
   display(line2);


   return 0;
}

我得到输出:

Normal constructor allocating ptr
Copy constructor allocating ptr.
Copy constructor allocating ptr.
Length of line : 10
Freeing memory 0x836c028
Copy constructor allocating ptr.
Length of line : 20
Freeing memory 0x836c028
Freeing memory 0x836c018
Freeing memory 0x836c008
Line line2 = line1; //assignment operator but copy constructor will be called implicitly
Line line3(line1); //copy constructor will be called explicitly

从现有对象创建新对象时,将调用复制构造函数。 这里line2line3是从现有的line1新创建的,所以即使你使用=也会调用copy构造函数。

Line line2; //default constructor is called
line2 = line1; //assignment operator is called

这里第一行用默认构造函数声明并初始化它,第二行是赋值。 所以在这里调用赋值运算符。 即,当将对象分配给已初始化的对象时,将调用赋值运算符。

赋值运算符不是必需的,但可能是合乎需要的。

如果你有一个Foo课,

Foo a;     // calls normal constructor
Foo b(a);  // calls copy constructor explicitely
Foo c = b; // calls copy constructor (initialization)
b = a;     // calls assignment operator

当作为函数调用(值参数)的结果复制对象时,将调用复制构造函数。

以下情况可能导致调用复制构造函数:

  • 按值返回对象时
  • 当一个对象通过值作为参数传递(到函数)时
  • 当一个物体被抛出时
  • 捕获对象时
  • 将对象放在括号括起的初始化列表中时

这些情况统称为复制初始化,相当于Obj x = a;

但是,不保证在这些情况下将调用复制构造函数,因为C ++标准允许编译器在某些情况下优化副本,一个例子是返回值优化(RVO)

术语返回值优化是指C ++标准中的特殊子句,它类似于: 实现可以省略由return语句产生的复制操作,即使复制构造函数具有副作用

RVO特别值得注意的是,允许通过C ++标准更改结果程序的可观察行为

可以使用以下两种技术之一为对象分配值:

  • 表达式中的显式赋值
  • 初始化

表达式中的显式赋值 (调用简单副本,而不是复制构造函数!)

Object a;
Object b;
a = b;//translates as Object::operator=(const Object&)thus a.operator=(b)is called 

初始化 (调用复制构造函数)

可以通过以下任何一种方式初始化对象。

一种。 通过声明

Object b = a; // translates as Object::Object(const Object&) 

通过函数参数

type function(Object a);

C。 通过函数返回值

Object a = function();

当有复制构造函数时,是否总是需要赋值运算符?

复制构造函数仅用于初始化,不适用于使用赋值运算符的赋值。

您的代码似乎可以正常工作,但有些情况可能会崩溃。 看看你的dtor,它释放了ptr指向的内存。 如果将对象分配给另一个对象,则默认赋值运算符将仅复制指针的值(浅拷贝),然后您有两个对象,每个对象都指向同一个内存块。 这就是问题。 不禁止这样做,但是在释放内存时需要注意等等。当您使用复制ctor获取深层复制时,您还需要使用赋值运算符来制作相同的深层复制。 这就是为什么存在三个规则(对于所有“复制/分配”场景的对象的统一行为)...

 // Line line2 = line1; // This also calls copy constructor 

是的,因为仅存在=字符并不表示调用赋值运算符。 正如cppreference.com所说:

在命名变量的复制初始化中,等号= ,与赋值运算符无关。 赋值运算符重载对复制初始化没有影响。

然后在代码中有以下行:

  Line line2(line1); // performed by copy assignment operator 

这个评论是错误的。 为什么要在这里调用复制赋值运算符?

但是,复制构造函数和复制赋值运算符之间存在一个重要的关系,那就是通常应该使用复制和交换习惯用另一个来实现。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM