2

Learning C++ initialization on cppreference I found the following (as value initialization "since C++11"):

1) if T is a class type with no default constructor or with a user-provided or deleted default constructor, the object is default-initialized;

2) if T is a class type with a default constructor that is neither user-provided nor deleted (that is, it may be a class with an implicitly-defined or defaulted default constructor), the object is zero-initialized and then it is default-initialized if it has a non-trivial default constructor;

...

This means that a class type can have:

  • no;
  • user-provided;
  • deleted;
  • implicitly-defined;
  • defaulted;

default constructor.

What does "no" default constructor mean? Don't class types always have one at least implicitly defined (or it is deleted)?

0

2 Answers 2

2

What does "no" default constructor mean? Don't class types always have one at least implicitly defined (or it is deleted)?

In case there's a user defined constructor, there's no implicitely defined default constructor (with no arguments).

The easiest way to declare one is to use the default keyword:

class MyClass {
public:
    MyClass(int y); // <<< No default constructor generated
    MyClass() = default; // <<< Force generation of default constructor
};
0

See this answer for an explanation of implicit default: https://stackoverflow.com/a/12340762/3616833

In simple terms, a constructor is default if it can be called with no arguments. A constructor is implicit(ly declared/defined) if it is not provided by the user but declared/defined.

A class can still be declared/defined with no default constructor if all of its constructors require at least one argument.

The default keyword creates a defaulted default constructor so is not the answer to the OP question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.