ledonter
ledonter

Reputation: 1719

"No default constructor" for a class type

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:

default constructor.

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

Upvotes: 2

Views: 1379

Answers (2)

NeoVance
NeoVance

Reputation: 404

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.

Upvotes: 0

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

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
};

Upvotes: 2

Related Questions