Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why explicitly declare classes special functions as "default" [duplicate]

What is the difference between explicitly declaring classes special functions default.

class Myclass
{
public:

    Myclass() = default;
    virtual ~Myclass() = default;

    Myclass(MyClass&&) = default;
    Myclass& operator=(MyClass&&) = default;

    Myclass(const MyClass&) = default;
    Myclass& operator=(const MyClass&) = default;
};

MyClass{};

What is the difference between this 2 declarations? Why explicitly specify the default behavioral functions as default??

like image 569
Eduard Rostomyan Avatar asked Jan 04 '23 21:01

Eduard Rostomyan


1 Answers

Because under certain conditions the compiler might not add the constructors, destructor or operators even though you may want the compiler-generated defaults. Then by using the explicit default designator the compiler will do that anyway.

You can find out more in e.g. this class reference.

like image 135
Some programmer dude Avatar answered Jan 26 '23 02:01

Some programmer dude