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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With