Classes: Delegating constructors


Starting with C++11, a constructor can call another constructor of the same class. Delegating constructors cannot be recursive.
Delegate in cases where the delegation makes sense eg. multiple constructors need to call the same function (checks, init etc.). For member initialization, use the in-class member initializer feature instead.

Don’t Repeat Yourself (DRY) principle

  • Keep the implementation in one place. Useful when the same members needs to be instantiated and/or the same function call (let’s say init() ) needs to be called for each constructor call.


  • Clean code (DRY principle) = avoid code duplication in constructors


  • Overusing this feature (long chains of constructor delegation) would become hard to read
  • A delegating constructor can contain only the delegated constructor into the list – no other data can be initialized by it


Quiz – Which of the following is a correct use of a delegating constructor?

Answer: First one.

You may also like...

Leave a Reply