C++ Constructors

Thameera Senanayaka

Allion Technologies

What is a constructor?

  • Initializes the members of a class
  • Same name as the class
  • No return value
  • Can be public, protected or private

Order of Construction

  • Base class and member constructors in the order of declration
  • If the class is derived from virtual base classes, it initializes the object's virtual base pointers
  • If the class has or inherits virtual functions, it initializes the object's virtual function pointers
  • Execute code in its function body

Demo

Member initializer lists

  • More efficient than using assignment operators in ctor body
class Box {
public:
    Box(int width, int length, int height) 
        : m_width(width), m_length(length), m_height(height) // member init list
    {}
    int Volume() {return m_width * m_length * m_height; }

private:
    int m_width;
    int m_length;
    int m_height;
};

Explicit constructors

  • If a constructor has only one parameter (or multiple parameters except one having default values), that parameter type can be converted to the class type.
  • Can lead to subtle but serious bugs

Demo

Default constructors

  • A constructor that can be called with no arguments
  • If no ctor is defined, a default ctor is provided by compiler
  • Default constructors are special
    • Box b;
    • Box b[10];
    • std::vector<Box>(10);
    • When a derived class ctor does not explicitly call base class ctor in intialization list

Copy constructors and copy assignment operators

My_Array first;           // initialization by default constructor
My_Array second(first);   // initialization by copy constructor
My_Array third = first;   // Also initialization by copy constructor
second = third;           // assignment by copy assignment operator
// Copy ctor
X(const X& other);

// Copy assignment operator
X& operator= (const X& other);

Rule of three

  • If a class defines one (or more) of the following three, it should probably explicitly define all three.
    • Destructor
    • Copy constructor
    • Copy assignment operator
  • If one of these had to be defined by the programmer, compiler-generated ones of others may not be what the programmer needs.

Move consturctor and move assignment operator

  • Introduced by C++11
  • Topic for a separate presentation
  • Rule of 5

Explicitly defaulted / deleted constructors

  • Disallow creating instances (eg: singleton)
  • Disallow copying
struct noncopyable
{
  noncopyable() {};

private:
  noncopyable(const noncopyable&);
  noncopyable& operator=(const noncopyable&);
};
  • C++11 version

Explicitly defaulted / deleted constructors

struct noncopyable
{
  noncopyable() =default;
  noncopyable(const noncopyable&) =delete;
  noncopyable& operator=(const noncopyable&) =delete;
};

Constructors in derived classes

  • Derived class ctors always call base class ctors
  • If a base class does not have a default ctor, you must supply base class ctor parameters in the derived class ctor

Demo

Thank you!

C++ Constructors

By Thameera

C++ Constructors

  • 1,410