delete
C++11 allows deleting functions which the compiler would have generated automatically. This practices supersedes the practice of making unwanted compiler generated member functions private. C++98 generates the following functions for every class if they haven’t been declared and are needed -
To prevent generation of these we can delcare them private. C++11 allows a better way to handle this, by marking them deleted i.e. -
class ABC {
public:
ABC();
ABC(ABC&) = delete; //no copy ctor
ABC& opertor=(ABC&) = delete; //no assignement operator
};
This can be extended to non-member functions. For example prevent overloads/implicit conversions -
int IsEvenNumber(int x);
int IsEvenNumber(bool) = delete;
int IsEvenNumber(char) = delete;
int IsEvenNumber(float) = delete;
This can be applied to prevent unwanted templated instantiations -
template<typename T> void Process(T * ptr);
template<> void Process<void>(void*) = delete; //no instantiations for void*
template<> void Process<void>(const void*) = delete; //no instantiations for const void*
template<> void Process<char>(char*) = delete; //no instantiations for char*
template<> void Process<char>(const char*) = delete; //no instantiatinos for const char *
override
C++11 allows specifying override keyword on member functions when overriding base class behavior in derived classes. Example -
class base {
public:
virtual void f1(int x);
};
class derived : public base {
public:
virtual void f1(int x) override; //possibly modify the behavior of f1
};
The use of override keyword tells the compiler that derived::f1 overrides base:;f1. In case there is a mismatch in declarations of f1, access specfier of f1 or for some other syntactical mistake if derived::f1 does not really override base::f1 then the compiler will flag this as an error.
final
The final keyword when applied to member functions prevents derived classes from overriding it and when applied to a class prevents any other class from inheriting it.
class base final {};
class derived:: public base {}; //Error
class base {
public:
virtual void f();
};
class derived: public base {
public:
virtual void f() override final; //Override f() in base class and prevent it from being overriden in further child classes
};
class derived2: public derived {
public:
virtual void f() override; //Error can’t override f() as it’s final in derived class
};
default
C++11 introduced the default keyword to explicitly state that you want the compiler generated version. Example -
class ABC {
public:
ABC() = default; //generate default ctor
ABC(const ABC&) = default; //generate default copy ctor
ABC& operator=(const ABC&) = default; //generate default copy assignement operator
ABC(ABC&&) = default; //generate default move ctor
ABC& operator=(ABC&&) = default; //generate default move assignement operator
virtual ~ABC() = default; //generate default dtor and make it virtual
};
C++11 allows deleting functions which the compiler would have generated automatically. This practices supersedes the practice of making unwanted compiler generated member functions private. C++98 generates the following functions for every class if they haven’t been declared and are needed -
- Default Ctor
- Dtor
- Copy Ctor
- Assignment Operator
To prevent generation of these we can delcare them private. C++11 allows a better way to handle this, by marking them deleted i.e. -
class ABC {
public:
ABC();
ABC(ABC&) = delete; //no copy ctor
ABC& opertor=(ABC&) = delete; //no assignement operator
};
This can be extended to non-member functions. For example prevent overloads/implicit conversions -
int IsEvenNumber(int x);
int IsEvenNumber(bool) = delete;
int IsEvenNumber(char) = delete;
int IsEvenNumber(float) = delete;
This can be applied to prevent unwanted templated instantiations -
template<typename T> void Process(T * ptr);
template<> void Process<void>(void*) = delete; //no instantiations for void*
template<> void Process<void>(const void*) = delete; //no instantiations for const void*
template<> void Process<char>(char*) = delete; //no instantiations for char*
template<> void Process<char>(const char*) = delete; //no instantiatinos for const char *
override
C++11 allows specifying override keyword on member functions when overriding base class behavior in derived classes. Example -
class base {
public:
virtual void f1(int x);
};
class derived : public base {
public:
virtual void f1(int x) override; //possibly modify the behavior of f1
};
The use of override keyword tells the compiler that derived::f1 overrides base:;f1. In case there is a mismatch in declarations of f1, access specfier of f1 or for some other syntactical mistake if derived::f1 does not really override base::f1 then the compiler will flag this as an error.
final
The final keyword when applied to member functions prevents derived classes from overriding it and when applied to a class prevents any other class from inheriting it.
class base final {};
class derived:: public base {}; //Error
class base {
public:
virtual void f();
};
class derived: public base {
public:
virtual void f() override final; //Override f() in base class and prevent it from being overriden in further child classes
};
class derived2: public derived {
public:
virtual void f() override; //Error can’t override f() as it’s final in derived class
};
default
C++11 introduced the default keyword to explicitly state that you want the compiler generated version. Example -
class ABC {
public:
ABC() = default; //generate default ctor
ABC(const ABC&) = default; //generate default copy ctor
ABC& operator=(const ABC&) = default; //generate default copy assignement operator
ABC(ABC&&) = default; //generate default move ctor
ABC& operator=(ABC&&) = default; //generate default move assignement operator
virtual ~ABC() = default; //generate default dtor and make it virtual
};