(C++ Object Model): Analyze the obj object file, the semantics of the constructor (on)

table of Contents

Default constructor

Introduce how to look at obj files under windows

When will the compiler help us synthesize the default constructor when necessary?

Add class objects for testing

Switch the order of MATX and MCTX and test again


Default constructor

  • Default constructor (default constructor): a constructor without parameters
    • The traditional belief is that if we don’t define any constructor ourselves, the compiler will implicitly and automatically define a default constructor for us. We call this constructor: " synthetic default constructor "
  • Conclusion: "Synthetic default constructor", only when necessary , the compiler will synthesize it for us, not necessarily or must be synthesized for us .
  • When is it necessary?
  • Each .cpp source file will be compiled to generate an .obj(.o) gcc -c under linux, and finally a lot of .obj(.o) files will be linked together to generate an executable.

Introduce how to look at obj files under windows

  • Source File
#include <iostream>
using namespace std;

class MBTX
{
public:
	int m_i;
	int m_j;

	void funct()
	{
		cout << "IAmVeryGood" << endl;
	}
};

int main()
{
	MBTX myb;
	return 1;
}
  • Generate solution

  • Right-click the file to open the folder

  • Enter the Debug directory and find the file with the same name as the extension .obj

  • Find the VS developer command line prompt

  • Enter the path of the .obj file

  • Use dumpbin to export the content of the .obj file into a viewable file my.txt. This my.txt format is generally considered to be COFF: Common Object File Format (Common Object File Format);
  • Drag to VS2019 to open
dumpbin /all project100.obj > my.txt

  • Next, check if the system has synthesized default constructor, MBTX::MBTX
  • Ctrl + F search, the results are as follows

  • The compiler does not synthesize the constructor, this situation is not necessary.

When will the compiler help us synthesize the default constructor when necessary?

  • This class MBTX does not have any constructor , but contains a member ma of the class type , and the class MATX to which the object ma belongs has a default constructor .
  • At this time, the compiler will generate a "synthetic default constructor" for this type of MBTX. The purpose of synthesis is to call the default constructor in MATX.
  • In other words: the compiler synthesizes the default MBTX constructor, and inserts code in it to call the default constructor of MATX ;
#include <iostream>
using namespace std;

class MATX
{
public:
	MATX() //默认构造函数
	{
		cout << "YYYYY" << endl;
	}
};

class MBTX
{
public:
	int m_i;
	int m_j;
	MATX ma;  //类类型成员变量

	void funct()
	{
		cout << "IAmVeryGood" << endl;
	}
};

int main()
{
	MBTX myb;   //输出YYYYY
	return 1;
}
  • Use the dumpbin tool (refer to the previous process)

Add class objects for testing

#include <iostream>
using namespace std;

class MCTX
{
public:
	MCTX() //默认构造函数
	{
		cout << "CCCCC" << endl;
	}
};

class MATX
{
public:
	MATX() //默认构造函数
	{
		cout << "YYYYY" << endl;
	}
};

class MBTX
{
public:
	int m_i;
	int m_j;
	MATX ma;  //类类型成员变量
	MCTX mc;  //类类型成员变量

	void funct()
	{
		cout << "IAmVeryGood" << endl;
	}
};

int main()
{
	MBTX myb;  //输出YYYYY  CCCCC
	return 1;  
}
  • Use dumpbin tool to test:

Switch the order of MATX and MCTX and test again

  • Output

  • Use dumpbin tool to test:

Guess you like

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