Tutorial by Examples

A class designed to be inherited-from is called a Base class. Care should be taken with the special member functions of such a class. A class designed to be used polymorphically at run-time (through a pointer to the base class) should declare the destructor virtual. This allows the derived parts of...
Bear in mind that declaring a destructor inhibits the compiler from generating implicit move constructors and move assignment operators. If you declare a destructor, remember to also add appropriate definitions for the move operations. Furthermore, declaring move operations will suppress the genera...
If you're writing a class that manages resources, you need to implement all the special member functions (see Rule of Three/Five/Zero). The most direct approach to writing the copy constructor and assignment operator would be: person(const person &other) : name(new char[std::strlen(other.n...
A default constructor is a type of constructor that requires no parameters when called. It is named after the type it constructs and is a member function of it (as all constructors are). class C{ int i; public: // the default constructor definition C() : i(0){ // member initial...
A destructor is a function without arguments that is called when a user-defined object is about to be destroyed. It is named after the type it destructs with a ~ prefix. class C{ int* is; string s; public: C() : is( new int[10] ){ } ~C(){ // destructor definition ...

Page 1 of 1