Vladimir Yanakiev Vladimir Yanakiev - 5 months ago 15
C++ Question

Usage of Initilizer list with default keyword

Why I cannot use default keyword after initialization list

class classA
int num;
classA():num(3) = default;


= default provides a definition of the constructor. Note that it doesn't provide the body, it provides a definition. The definition of a constructor includes both the mem-initialiser-list and the body. So if you want your own mem-initialiser-list, you must provide the entire definition yourself.

Also note that there is zero problem with doing that. Just write {} instead of = default. A default constructor defined with = default performs exactly the same operations as one defined with {}.

The only difference between these is that a constructor defined with = default right at its declaration is not considered user-provided and thus allows the class to be a trivial class. But since you want something non-trivial to happen in the constructor, you get exactly what you want by using {}.