user3027198 user3027198 - 3 months ago 9
C++ Question

Will default constructor still be created if I explicitly define a constructor with arguments?

Will default constructor still be created if I explicitly define a constructor with arguments but no default constructor?

Thank you!

Answer

No, you have to explicitly provide the default constructor in that case.

Although you can (from C++11 onwards), for a class Foo, use the syntax

Foo() = default;

within the class declaration which reintroduces the compiler-generated default constructor. (Note you need to put this in the public section for exact equivalence).

Alternatively, if you provide default arguments to all the constructor parameters, then it becomes the default constructor.