Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is assembly code created for Default Constructor in C++

If I do not define a default constructor in a class in C++ , or any other constructors, I have read that the compiler creates a default constructor for you. But I created a test class, compiled it to assembly code and checked to find that nothing of the sort is created.
Can someone clarify how the code for default constructor is created, or if it is created in the first place?

like image 671
woodstok Avatar asked Mar 05 '26 17:03

woodstok


1 Answers

Default constructor is created if you need it, e.g:

class Foo {
  std::string s;
};

...
Foo f;

12.1:

The default constructor (12.1), copy constructor and copy assignment operator (12.8), and destructor (12.4) are special member functions. The implementation will implicitly declare these member functions for a class type when the program does not explicitly declare them, except as noted in 12.1. The implementation will implicitly define them if they are used, as specified in 12.1, 12.4 and 12.8.

Also, if your class doesn't require anything to be done in the constructor, a compiler may choose to not generate the code, even though by the standard the constructor should exist.

like image 98
Erik Avatar answered Mar 08 '26 07:03

Erik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!