The construction operation of C++ default constructor

1 member object with default constructor

1.1 Behavior

When the external object contains a member object, the compiler will automatically add the call when the member object constructor is not explicitly called;

1.2 Sample code

#include <iostream>
#include <string>
using namespace std;
class A
{
    
    
public:	
	A() {
    
     cout << "call A()" << endl; }
	A(int value) {
    
     cout << "call A(value)" << endl; }
};
class B
{
    
    
private:
	A a;
	int value;
	string name;
public:
	B() {
    
    
		name = "aloha";
	}
	void show() 
	{
    
    
		cout << "B value=" << value << ",name=" << name <<endl; 
	};
};

int main()
{
    
     
	B b;
	b.show();
}

1.3 Output

call A()
B value=-858993460,name=aloha

Note:
If the default constructor of A in the comment code, the compilation will not pass.

Need to explicitly call the non-default constructor of A in the constructor of B through the initialization list

	B():a(0) {
    
     
		name = "aloha";
	}

2 The parent type has a default constructor

2.1 Behavior

In the constructor of the subtype, the constructor of the parent type is automatically called.

2.2 Sample code 1

#include <iostream>
#include <string>
using namespace std;
class A
{
    
    
public:	
	A() {
    
     cout << "call A()" << endl; }
	A(int value) {
    
     cout << "call A(value)" << endl; }
}; 

class AA:public A
{
    
     
	int value;
	string name;
public:
	AA()  {
    
     
		value = 0;
		name = "aloha";
	}
	void show()
	{
    
    
		cout << "AA value=" << value << ",name=" << name << endl;
	};

};

int main()
{
    
     
	AA aa;
	aa.show();
}

2.3 Output 1

call A()
AA value=0,name=aloha

2.4 Sample code 2

#include <iostream>
#include <string>
using namespace std;
class A
{
    
    
public:	
	A() {
    
     cout << "call A()" << endl; }
	A(int value) {
    
     cout << "call A(value)" << endl; }
}; 

class AA:public A
{
    
     
	int value;
	string name;
public:
	AA()  
	{
    
     
		value = 0;
		name = "aloha";
	}
	AA(int value)
	{
    
    
		this->value = value;
		name = "aloha";
	}
	void show()
	{
    
    
		cout << "AA value=" << value << ",name=" << name << endl;
	};

};

int main()
{
    
     
	AA aa(5);
	aa.show();
}

2.5 Output 2

call A()
AA value=5,name=aloha

3 The case of both

3.1 Behavior

First call the default construction method of the parent class, and then call the default construction method of the member object

3.2 Sample code

#include <iostream>
#include <string>
using namespace std;
class A
{
    
    
public:	
	A() {
    
     cout << "call A()" << endl; }
	A(int value) {
    
     cout << "call A(value)" << endl; }
}; 
class B
{
    
    
public:
	B() {
    
     cout << "call B()" << endl; }
	B(int value) {
    
     cout << "call B(value)" << endl; }

};
class AAB:public A
{
    
     
	int value;
	string name;
	B b;
public:
	AAB()
	{
    
     
		value = 0;
		name = "aloha";
	}
	AAB(int value) 
	{
    
    
		cout << "AAB start" << endl;
		this->value = value;
		name = "aloha";
		cout << "AAB end" << endl;
	}
	void show()
	{
    
    
		cout << "AAB value=" << value << ",name=" << name << endl;
	};

};

int main()
{
    
     
	AAB aab(5);
	aab.show();
}

3.3 Output

call A()
call B()
AAB start
AAB end
AAB value=5,name=aloha

4 references

"In-depth exploration of the C++ object model", by Stanley B.Lippman, translated by Hou Jie, Electronic Industry Press, 2012;

Guess you like

Origin blog.csdn.net/skytering/article/details/105908949
Recommended