Copy构造函数和赋值运算符中的混淆 [英] Confusion in Copy Constructor and assignment operator

查看:95
本文介绍了Copy构造函数和赋值运算符中的混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在已经累了但仍然无法理解复制构造函数和赋值运算符。

最后,我在这里,在我最好的网站之一,希望我能清除我的所有概念。



现在我知道默认复制构造函数执行浅复制,但是当我们使用赋值运算符复制对象时,它将成为深层复制...... ???



我们假设,我们有一个类,MyClass和一个构造函数在其中定义,没有任何参数,main编码为:< br $>


 MyClass c1( 欢迎); 
MyClass c2;
c2 = c1;





所以,第3行c1 = c2是深拷贝?

如果它也是浅层副本,那么c2 = c1和MyClass c2(c1)(简单复制构造函数)之间的区别是什么。



如果我将其编辑为会发生什么:

 MyClass c1( < span class =code-string> Welcome); 
MyClass c2 = c1;





先谢谢...: - )

解决方案

无论是调用默认的复制构造函数还是赋值运算符,编译器默认情况下都只能创建浅复制。浅拷贝意味着使用该成员的赋值运算符单独复制类的每个成员。这在指针的情况下很重要,因为指针被复制但指向的不是指针。如果分配并使用了内存,则会产生问题。



  #include   <   string  >  
#include < iostream >
使用 命名空间标准;

// 使用指向内存的字符串
class myClass
{
public
myClass( const char * cstring){mystring = new char [ 30 ]; strcpy(mystring,cstring);};
myClass():mystring(NULL){};
~myClass(){ delete [] mystring;};

char * mystring;
};

// 使用std :: string class
class myClass2
{
public
myClass2( const char * cstring){mystring = cstring;};
myClass2():mystring( ){};
~myClass2(){};

string mystring;
};

int main()
{

myClass c1( 欢迎);
myClass c2;
c2 = c1;
// 浅拷贝 - c2 :: mystring指针的赋值与c1 :: mystring
// 指针指向相同的内存 - DANGER
// 如果你在调试器中检查c1和c2,这是骗人的,因为它们似乎都
// 包含欢迎 - 不是这样 - 它们都有一个指向相同的指针
// memory。

// 必须谨慎管理。例如:
// #define TEST
#ifdef TEST
myClass * pc2 = new myClass;
* pc2 = c1;

cout<< c1.mystring<< ENDL; // 一切都很好。
cout<< pc2-> mystring<< ENDL; // 一切都很好。
delete pc2;
cout<< c1.mystring<< ENDL; // 问题 - 为什么?
// 浅层复制的后果。
#endif

myClass c1a( Welcome);
myClass c2a = c1a; // 与上述相同 - 不同构造相同的结果。


// 现在使用myClass2
myClass2 c1_2( 欢迎);
myClass2 c2_2;
c2_2 = c1_2;
// 浅拷贝 - 但是c2 :: mystring现在是c1 :: mystring的副本,因为
// std :: string的赋值运算符。

// 现在
// #define TEST2
#ifdef TEST2
myClass2 * pc2_2 = new myClass2;
* pc2_2 = c1_2;

cout<< c1_2.mystring<< ENDL; // 一切都很好。
cout<< pc2_2-> mystring<< ENDL; // 一切都很好。
delete pc2_2;
cout<< c1_2.mystring<< ENDL; // 没问题。
#endif


return 0 ;
}





这是一个非常好的展示:

http://www.learncpp.com/cpp-tutorial/912-shallow-vs-deep-copying/ [ ^ ]


考虑以下C ++程序。

  #include   <   iostream  >  
#include < stdio.h >
使用 命名空间标准;
class 测试
{
public
Test (){}
测试( const 测试& t)
{
cout<< 复制构造函数<< endl;
}
测试& operator =( const 测试& t)
{
cout<< 分配运算符名为<< endl;
}
};
int main()
{
测试t1,t2;
t2 = t1;
测试t3 = t1;
getchar();
return 0 ;
}



输出:

赋值运算符
复制构造函数名为



当从现有对象创建新对象

时,将调用复制构造函数,作为副本

的现有对象对象(见本次G-Fact)。当

已经初始化的对象被另一个现有对象分配一个新的

值时,调用
赋值运算符。

 t2 = t1;  //  调用赋值运算符,与t2.operator =(t1); 
相同试验t3 = t1; // 调用复制构造函数,与Test t3(t1);

I am now tired but still not able to understand Copy constructor and assignment operator clearly.
And at last, i am here, in one of my best site, in a hope that i'll clear all my concepts.

Now coming to the point, i know default copy constructor do shallow copying but when we copy object using assignment operator then will it become a deep copy...???

Let us suppose, we have a class, MyClass and a constructor is defined in it without any argument, and main is coded as:

MyClass c1("Welcome");
MyClass  c2;
c2 = c1;



So, the 3rd line "c1=c2" is deep copy??
And if it is also a shallow copy that what's the difference between this "c2=c1" and "MyClass c2(c1)"(Simple copy constructor).

And what will happen if i'll edit it as:

MyClass c1("Welcome");
MyClass  c2 = c1;



Thanks in advance...:-)

解决方案

Whether you are calling a default copy constructor or assignment operator the compiler can only create a shallow copy by default. A shallow copy means that each member of the class is copied individually using the assignment operator for that member. This is important in the case of pointers because the pointer is copied but what it points to is not. If memory has been allocated and used this creates a problem.

#include <string>
#include <iostream>
using namespace std;

// Use pointer to memory for string
class myClass 
{
public:
	myClass(const char *cstring) { mystring = new char[30]; strcpy(mystring, cstring);};
	myClass() : mystring(NULL) {};
	~myClass() {delete [] mystring;};

	char *mystring;
};

// Use std::string class
class myClass2 
{
public:
	myClass2(const char *cstring) { mystring = cstring;};
	myClass2() : mystring("") {};
	~myClass2() {};

	string mystring;
};

int main()
{

myClass c1("Welcome");
myClass  c2;
c2 = c1; 
// Shallow copy - c2::mystring pointer is assigned same value as c1::mystring 
// pointer so points to same memory - DANGER
// This is deceptive if you examine c1 and c2 in the debugger because they both seem
// to contain "Welcome" - not so - they both have a pointer which points to the same
// memory.

// This must be carefully managed. For example:
//#define TEST
#ifdef TEST
myClass *pc2 = new myClass;
*pc2 = c1;

cout << c1.mystring << endl; // All good.
cout << pc2->mystring << endl; // All good.
delete pc2;
cout << c1.mystring << endl; // Problem - why?
// Consequence of shallow copy.
#endif

myClass c1a("Welcome");
myClass  c2a = c1a; // Same as above - different construct same result.


// Now using myClass2
myClass2 c1_2("Welcome");
myClass2  c2_2;
c2_2 = c1_2; 
// Shallow copy - but c2::mystring is now a copy of c1::mystring because
// of assignment operator for std::string.

// Now
//#define TEST2
#ifdef TEST2
myClass2 *pc2_2 = new myClass2;
*pc2_2 = c1_2;

cout << c1_2.mystring << endl; // All good.
cout << pc2_2->mystring << endl; // All good.
delete pc2_2;
cout << c1_2.mystring << endl; // No Problem.
#endif


 return 0;
 }



This is a very good expose:
http://www.learncpp.com/cpp-tutorial/912-shallow-vs-deep-copying/[^]


Consider the following C++ program.

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


Output:

Assignment operator called
Copy constructor called


Copy constructor is called when a new object
is created from an existing object, as a copy
of the existing object (see this G-Fact). And
assignment operator is called when an
already initialized object is assigned a new
value from another existing object.

t2 = t1;  // calls assignment operator, same as "t2.operator=(t1);"
Test t3 = t1;  // calls copy constructor, same as "Test t3(t1);"


这篇关于Copy构造函数和赋值运算符中的混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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