(C++ Object Model): Copy constructor semantics

table of Contents

Reference

① If a class A has no copy constructor, but contains a member variable m_ctb of class type CTB. This type of CTB contains a copy constructor, so when the code involves a copy construction of class A, the compiler will synthesize a copy constructor for class A.

② If a class CTBSon does not have a copy constructor, but it has a parent class CTB, and the parent class has a copy constructor, when the code involves a copy structure of the class CTBSon, the compiler will synthesize a copy constructor for CTBSon and call The copy constructor of the parent class.

③ If a class CTBSon does not have a copy constructor, but the class declares or inherits a virtual function, when the code involves a copy structure of the class CTBSon, the compiler will synthesize a copy constructor for CTBSon

The parent class contains virtual functions, and the subclass does not have a copy constructor, the compiler synthesizes the copy constructor

④ If a class does not have a copy constructor, but the class contains a virtual base class. When the code involves the copy construction of the class, the compiler will synthesize a copy constructor for the class


Reference

  • Traditionally, everyone thought: if we did not define a copy constructor of our own, the compiler would help us synthesize a copy constructor.
  • This synthesized copy constructor is also synthesized by the compiler when necessary.
#include <iostream>
using namespace std;

class A 
{
public:
	int m_test;
};
int main()
{
	A mya1;
	mya1.m_test = 15; 
	A mya2 = mya1; 
	return 1;
}
  • Debug: object mya2 value is also 15

  • Is the copy constructor synthesized at this time? Use dumpbin to view

  • Found no synthetic copy constructor
  • This mya2.m_test = 15, this is actually a trick inside the compiler:
    • Member variable initialization techniques , such as simple types like int, are copied directly by value. The compiler does not need to synthesize the copy constructor to help us do this.
  • Add simple class type members to the class
#include <iostream>
using namespace std;

class ASon
{
public:
	int m_testson;
};
class A 
{
public:
	int m_test;
	ASon asubobj;
};
int main()
{
	A mya1;
	mya1.m_test = 15; 
	mya1.asubobj.m_testson = 120;
	A mya2 = mya1; 
	return 1;
}
  • Debugging: the object mya2 value is also 15, m_testson is 120

  • Is the copy constructor synthesized at this time? Use dumpbin to view

  • summary
    • a) A mya2 = mya1; is to copy and construct an object.
    • b) We did not write a copy constructor of class A, nor did the compiler help us generate a copy constructor.
    • c) We found that some member variable values ​​of the mya1 object were indeed copied to mya2. This is some implementation methods of directly copying data inside the compiler
      • For example, there is a member variable asubobj of class type ASon in class A, and each member variable of class ASon will be copied recursively.
  • In some cases, if we don't write our own copy constructor, the compiler will help us synthesize a copy constructor.
  • Under what circumstances will the compiler help us synthesize a copy constructor? So what does the copy constructor synthesized by this compiler do?

① If a class A has no copy constructor, but contains a member variable m_ctb of class type CTB. This type of CTB contains a copy constructor, so when the code involves a copy construction of class A, the compiler will synthesize a copy constructor for class A.

#include <iostream>
using namespace std;

class CTB
{
public:
	CTB(const CTB&)
	{
		cout << "CTB()的拷贝构造函数执行了" << endl;
	}
	CTB()
	{
	}
};
class ASon
{
public:
	int m_testson;
};
class A 
{
public:
	int m_test;
	ASon asubobj;
	CTB m_ctb;
};
int main()
{
	A mya1;
	mya1.m_test = 15; 
	mya1.asubobj.m_testson = 120;
	A mya2 = mya1; 
	return 1;
}
  • Output result

  • dumpbin test:

  • The copy constructor synthesized by the compiler often does some special things (such as calling the copy constructor of its class type member). If it’s just a copy of some class member variable values, the compiler does not need to synthesize a copy constructor to do it, the compiler does it internally.
  • ②If a class CTBSon does not have a copy constructor, but it has a parent class CTB, and the parent class has a copy constructor, when the code involves a copy structure of the class CTBSon, the compiler will synthesize a copy constructor for CTBSon and call The copy constructor of the parent class.

#include <iostream>
using namespace std;

class CTB
{
public:
	CTB(const CTB&)
	{
		cout << "CTB()的拷贝构造函数执行了" << endl;
	}
	CTB()
	{
	}
};
class CTBSon :public CTB
{
public:
	
};

int main()
{
	CTBSon myctbson1;
	CTBSon myctbson2 = myctbson1;
	return 1;
}
  • Output result:

  • dumpbin test:

③ If a class CTBSon does not have a copy constructor, but the class declares or inherits a virtual function, when the code involves a copy structure of the class CTBSon, the compiler will synthesize a copy constructor for CTBSon

#include <iostream>
using namespace std;


class CTBSon 
{
public:
	virtual void mvirfunc() {}
};

int main()
{
	CTBSon myctbson1;
	CTBSon myctbson2 = myctbson1;
	return 1;
}
  • dumpbin test:

  • Insert statements into this copy constructor:
??_7CTBSon@@6B@ (const CTBSon::`vftable')
  • The meaning of this statement is to set the virtual function table pointer value of the class object myctbson2.

The parent class contains virtual functions, and the subclass does not have a copy constructor, the compiler synthesizes the copy constructor

#include <iostream>
using namespace std;

class CTB
{
public:
	virtual void mvirfunc() {}
};

class CTBSon :public CTB
{
public:
	
};

int main()
{
	CTBSon myctbson1;
	CTBSon myctbson2 = myctbson1;
	return 1;
}
  • dumpbin test:

④ If a class does not have a copy constructor, but the class contains a virtual base class. When the code involves the copy construction of the class, the compiler will synthesize a copy constructor for the class

#include <iostream>
using namespace std;

class Grand //爷爷类
{
public:
};
class A :virtual public Grand
{
public:
};
class A2 :virtual public Grand
{
public:
};
class C :public A, public A2
{
public:
};

int main()
{
	C cc;
	C cc2 = cc;
	return 1;
}
  • dumpbin test:

  • Explore the situation of other compilers synthesizing copy constructor

Guess you like

Origin blog.csdn.net/baidu_41388533/article/details/108652274