Search code examples
c++constructordefault-constructor

In which cases there is no constructor at all, even a default constructor?


In this book I am currently reading I ran across this:

A class doesn't need a constructor. A default constructor is not needed if the object doesn't need initialization.

Am I correct in inferring from the above that the compiler does not generate a default constructor for the class/structure in some cases? If yes, what are those cases? I will venture and say POD is probably one. Are there any other?

EDIT: I have changed the title as the original title gave the meaning that I asked when was a default constructor not defined instead of asking when does a class not have a constructor at all.


Solution

  • A class doesn't need a constructor. A default constructor is not needed if the object doesn't need initialization.

    I think the author is talking about this situation:

    some_type some_function () {
       POD_type this_is_intentionally_uninitialized;
       ...
    }
    

    Under some circumstances a constructor won't be called, period. As soon as you write a constructor you don't have a POD class, so now the constructor will be called.

    Whether it is a good or bad thing to have an object running around that contains random, uninitialized data is a different question entirely.