Search code examples
c++default-constructor

Why is the Compiler-Emitted C++ Default Constructor "bad"?


Could someone please explain what is meant by the following?

You must define a default constructor if your class defines member variables and has no other constructors. Otherwise the compiler will do it for you, badly.

What are they referring to as "badly"?


Solution

  • From the expansion of that link:

    "The reason for this is that if you have no other constructors and do not define a default constructor, the compiler will generate one for you. This compiler generated constructor may not initialize your object sensibly."