为什么我们需要复制构造器& C ++中的赋值运算符? [英] Why we need the Copy Constructor & the Assignment Operator in C++ ?

查看:98
本文介绍了为什么我们需要复制构造器& C ++中的赋值运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经看了很多文章,但都无法对两者都有任何具体的想法.

我是说-
-为什么我们需要这两个[实际用法]?
-这两个可以替代吗?
-何时必须使用它们?

请详细指导我(实用用法\主要优点).

谢谢

Hi,

I had gone through number of articles, but unable to get any concrete idea on both.

I mean to say -
- Why we need [practical usage] these two?
- Is there any substitution to these two?
- When it is compulsory to use them?

Kindly please guide me in detail (Practical usage\main advantages).

Thanks

推荐答案

我认为在CP中已经解释了很多次. 复制构造函数是一种特殊的构造函数,它会从现有对象中初始化一个新对象.编译器会创建一个默认的复制构造函数,如果您不创建它,则会一点一点地复制您的类数据.请考虑此类(不是专业密码).
I Think was explained lot of times in CP.But not by me so i am taking this one,
A copy constructor is a special constructor that initializes a new object from an existing object.Compiler will creates a default copy constructor which copies your class data bit by bit, if you wont create it.Then why we have to re-create a Copy constructor?consider this class(not a professional code).
Class A
{
    public int *p;
    A(){ p = new int;}
    ~A(){
         delete p;
     }
};
int main()
{
   A a;
   A b = a; //default copy constructor provided by compiler,which exactly copies a.p pointer to to b.p;
   return 0;
};


但是有一个大问题.当main将要死亡时,它将清除所有堆栈变量.因此,可能首先调用"a"析构函数,并删除"ap".接下来将调用b析构函数,然后再次尝试删除已经在a中删除的bp(请注意ap和bp指向相同的指针).这将导致运行时错误.要解决此问题,我们必须创建一个复制构造函数和Assignment运算符(在不同的场景中称为).因此,Class A复制构造函数将是


But there is one big problem.when main is going to die,it will clean all stack variables.So may be first "a" destructor will be called and "a.p" is deleted.Next b destructor will be called,which again tries to delete b.p which is already deleted in a(Note thst a.p and b.p pointes same pointer).This will cause an run time error.To overcome this problem we have to create a copy aonstructor and Assignement operator(which is called in different scenarios).So the Class A copy constructor will be,

class A::A(const class A& RefA)
{
   p = new int;    // create a new p 
   *p = *RefA.p; // copy the value
}


因此,上面示例中的a,b现在将具有不同的p指针.赋值运算符的功能也相同,但目标有所不同.

在三种常见情况下,将调用复制构造函数而不是赋值运算符:
1.实例化一个对象并用另一个对象的值对其进行初始化时(如上例所示).
2.按值传递对象时.
3.按值从函数返回对象时.

在其他情况下,例如


So now both a, b in the above example will have different p pointers.Assignement operator functionality is also same.But there aim is defferent.

There are three general cases where the copy constructor is called instead of the assignment operator:
1. When instantiating one object and initializing it with values from another object (as in the example above).
2. When passing an object by value.
3. When an object is returned from a function by value.

In other cases like,

A a;
a = b; 


一个赋值运算符称为


an Assignment operator is called


我们需要Copyconstructor从另一个对象初始化一个对象,并且通常我们使用赋值运算符来调用复制构造函数.
We need Copyconstructor to initialize one object from other object, And generally we use assignment operator to invoke copy constructor.


Hi venkat

感谢您对laxmikanth的协助(实际上,当我们需要复制构造函数/assignmnet oprator时,您节省了我很多时间来键入和解释您的情况)

Venkat解释了Deep Copy的情况(我们需要获取具有指针数据成员的对象的副本)

当我们将类设计为"SingleTon"时,复制构造函数/赋值运算符的另一个需求是

SingleTon:在整个应用会话期间只能有一个排他对象的类

因此,在我们的类不允许对象的第二个副本的情况下,我们必须重写Copy构造函数/赋值运算符,以便客户端无法通过将对象复制/辅助到新对象来创建调用的另一个实例

谢谢
Sanjay
Hi venkat

thanks for assisting laxmikanth ( actually you saved my lot of time of typing and explaining your case when we need Copy constructor / assignmnet oprator )

Venkat has explained the case of Deep Copy ( where we need to take a copy of object which has pointer datamember )

Another need of Copy constructor / assignment operator comes when we are designing our class as "SingleTon"

SingleTon: a class which can have only one exclusive object thrughout the application session

so in the case when our class should not allow 2nd copy of object then we have to override Copy constructor / assignment operator so that client can not create another instance of the call by copying/assining the abject to a new one

Thank You
Sanjay


这篇关于为什么我们需要复制构造器& C ++中的赋值运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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