Delete
In C++98, to prevent the compiler from generating default class member functions, we had to declare them private with an empty implementation. That's because the default functions were public by default. In C++11, the functions can be marked 'delete' explicitly instead of using the hacky C++98 method.
Additionally, delete keyword can be used to prevent automatic overloading. Example -
class BigObject
{
private:
int x;
public:
//allow default construction
BigObject();
//prevent copy construction
BigObject(const BigObject &) = delete;
//allow move construction
BigObject(BigObject&&);
//prevent copy assignment
BigObject& operator=(const BigObject&) = delete;
//allow move assignment
BigObject& operator=(BigObject&&);
void AddToX(short s) {x += s;}
//prevent member function overload to accept int
void AddToX(int) = delete;
};
Default
The default keyword explicitly tells the compiler to generate default implementations. In addition it allow tweaking other attributes. Example -
class BigObject
{
private:
int x;
protected:
/* generate default Ctor with protected scope instead of public. Although another Ctor has been declared below, still generate the default Ctor. Otherwise the compiler wouldn't generate this Ctor. */
BigObject() = default;
BigObject(int a): x(a) {};
public:
/* generate default Dtor with virtual specifier */
virtual ~BigObject() = default;
/* generate default copy Ctor accepting non-const reference instead of const reference */
BigObject(BigObject &) = default;
};
In C++98, to prevent the compiler from generating default class member functions, we had to declare them private with an empty implementation. That's because the default functions were public by default. In C++11, the functions can be marked 'delete' explicitly instead of using the hacky C++98 method.
Additionally, delete keyword can be used to prevent automatic overloading. Example -
class BigObject
{
private:
int x;
public:
//allow default construction
BigObject();
//prevent copy construction
BigObject(const BigObject &) = delete;
//allow move construction
BigObject(BigObject&&);
//prevent copy assignment
BigObject& operator=(const BigObject&) = delete;
//allow move assignment
BigObject& operator=(BigObject&&);
void AddToX(short s) {x += s;}
//prevent member function overload to accept int
void AddToX(int) = delete;
};
Default
The default keyword explicitly tells the compiler to generate default implementations. In addition it allow tweaking other attributes. Example -
class BigObject
{
private:
int x;
protected:
/* generate default Ctor with protected scope instead of public. Although another Ctor has been declared below, still generate the default Ctor. Otherwise the compiler wouldn't generate this Ctor. */
BigObject() = default;
BigObject(int a): x(a) {};
public:
/* generate default Dtor with virtual specifier */
virtual ~BigObject() = default;
/* generate default copy Ctor accepting non-const reference instead of const reference */
BigObject(BigObject &) = default;
};