如果复制构造函数和赋值运算符都在执行相同的任务,即(成员明智的复制),那么为什么我们单独使用它。 [英] If both copy constructor and assignment operator are doing same task i.e. (member wise copy) then why we are using it separately.

查看:82
本文介绍了如果复制构造函数和赋值运算符都在执行相同的任务,即(成员明智的复制),那么为什么我们单独使用它。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果复制构造函数和赋值运算符都在执行相同的任务,即(成员明智的复制)那么为什么我们单独使用它。



If both copy constructor and assignment operator are doing same task i.e. (member wise copy) then why we are using it separately.

class A
{

};
int main()
{
A a2;
A a2=a1;  //copy const.
A a3;
a3=a2;   //assignment opeartor

}





i认为两者都会做同样的工作然后为什么我们不只使用一个概念复制const或赋值运算符





如果有背后的概念,请帮帮我



提前致谢



[edit]删除了SHOUTING,添加了代码块 - OriginalGriff [/ edit]



i think both will do same job then why we are not using only one concept either copy const or assignment operator


Please help me if there is concept behind this

Thanks in advance

[edit]SHOUTING removed, Code block added - OriginalGriff[/edit]

推荐答案

复制构造函数和赋值运算符之间存在重要差异。复制构造函数始终以未初始化的对象开始,而赋值运算符以有效对象开始。



当对象分配资源时,这种差异变得很重要,例如一大块记忆。一个小例子可以说清楚这一点。想象一下,你的A类包含指向一些已分配内存的指针:

There is an important difference between a copy constructor and the assignment operator. A copy constructor always starts out with an un-initialized object, while the assignment operator starts with a valid object.

This difference becomes important when an object has allocated resources, for example a chunk of memory. A little example will make that point clear. Imagine your class A contains a pointer to some allocated memory:
class A
{
    char* m_string;
};



在构造函数中,您必须初始化此成员,例如


In the constructors you will have to initialize this member, for example

class A
{
public:
    A (const A& other)
    {
        m_string = new char [other.m_string.strlen() + 1];
        strcpy (m_string, other.m_string);
        // yes, there are better ways to do that!
    }
    char* m_string;
};



现在考虑一个赋值运算符。首先必须解除分配可能已经分配的任何内存,然后进行分配。


Now consider an assignment operator. It first has to de-allocate any memory that might have been allocated and then do the assignment.


nv3(解决方案1)和Prakash257(解决方案2)都是正确的,因为代码对于两个运营商可能需要不同,并且运营商在不同的情况下被调用。

我想添加如果您编写自己的复制构造函数和赋值运算符,则可以通过从复制构造函数调用赋值运算符来避免重复代码,例如: g。:

nv3 (solution 1) and Prakash257 (solution 2) are both correct, in that the code for the two operators may need to be different, and that the operators are called under different
circumstances.

I'd like to add that, if you write your own copy constructor and assignment operator, you can avoid duplicate code by calling the assignment operator from the copy constructor, e. g.:
class A{
   int num;
   char *name;
public:
   A() :
      num(0),
      name(nullptr)  // never forget to intialize that pointer!
   {
   }
   A(const A& a) :
      name(nullptr)  // never forget to intialize that pointer!
   {
      operator=(a);
   }
   ~A()
   {
      delete [] name; // beware: this would fail with an uninitialized pointer!
   }
   A& operator=(const A& a)
   {
      delete [] name; // beware: this would fail with an uninitialized pointer!
      name = nullptr;
      if (a.name != nullptr) {
         name = new char[strlen(a.name)+1];
         strcpy(name, a.name);
      }
      num = a.num;
      return *this;
   }
   int get const { return num; }
   int set(int n) { num = n; }
   ...
};



当然,在这个例子中,你并没有真正节省太多。但是,如果要复制10个或更多数据成员,它可能仍然很方便。


Of course, in this example you don't really save much. But it might still be handy when you have 10 or more data members to copy.


#include "iostream"
#include "stdio.h"
using namespace std; 
class CopyConstructor
{
  public:
    CopyConstructor () { } ;
    CopyConstructor(const CopyConstructor &t)
     {
       cout<<"Copy constructor called "<<endl;
     }
    CopyConstructor& operator = (const CopyConstructor &t)
    {
      cout<<"Assignment operator called "<<endl;   
    }
};
int main()
{
  CopyConstructor obj1, obj2;
  obj2 = obj1;
  CopyConstructor obj3 = obj1;
  getchar();
  return 0;
}



输出:

赋值运算符名为

复制构造函数名为



说明:

复制构造函数在从现有对象创建新对象时调用,如现有对象的副本。当已初始化的对象从另一个现有对象分配新值时,将调用赋值运算符

obj2 = obj1; //调用赋值运算符,与obj2.operator =(obj1);相同。

CopyConstructor obj3 = obj1; //调用复制构造函数,与CopyConstructor obj3(obj1);


Output:
Assignment operator called
Copy constructor called

Explanation:
Copy constructor is called when a new object is created from an existing object, as a copy of the existing object. And assignment operator is called when an already initialized object is assigned a new value from another existing object.
obj2 = obj1; // calls assignment operator, same as "obj2.operator=(obj1);"
CopyConstructor obj3 = obj1; // calls copy constructor, same as "CopyConstructor obj3(obj1);"

这篇关于如果复制构造函数和赋值运算符都在执行相同的任务,即(成员明智的复制),那么为什么我们单独使用它。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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