mardi 21 mars 2023

Why explicitly delete the constructor allowing creating object using initialization {} [duplicate]

This code works, with the deleted constructor.

The class created with deleted constructor should not allow to the creation of an object for the class. Mainly such classes are used for static scope.

But C++ allows deleted constructor objects can be created using initialization. What is the point of explicit constructor not being allowed but initialization is allowed?

#include <iostream>

class A {
    A() = delete;
    
    A(const A&) = delete;
    A(A&&) = delete;
    
public:
    void function() { std::cout << "A\n"; }
};

int main()
{
    A a{};  // this works.
    
    a.function(); 
}

Aucun commentaire:

Enregistrer un commentaire