10

I see some code in a codebase I'm working on that looks like:

 ZfooName::ZfooName(int magoo)
    : ZfooName()
 {
    fGoo = magoo;
 }

I'm assuming this is a C++11 feature, since it breaks in VS2012, but what does it mean?

easythrees
  • 1,530
  • 3
  • 16
  • 43
  • If ZfooName has a default constructor taking no arguments, this calls it. It is not a C++11 feature. (NVM, it is C++11, I just thought it wasn't). – jwimberley Aug 22 '18 at 18:37
  • 1
    Possible dupe? https://stackoverflow.com/questions/26199431/why-did-c11-introduce-delegating-constructors It's not the same question, but the answers contain good info? – Barry Aug 22 '18 at 18:41
  • 1
    Also good information: https://stackoverflow.com/questions/308276/can-i-call-a-constructor-from-another-constructor-do-constructor-chaining-in-c – Tas Aug 23 '18 at 01:08

1 Answers1

21

This is a new feature in C++11. It's called a delegating constructor.

The constructor calls the default constructor first (the constructor that is being delegated to). After the default constructor returns, the body of the delegating constructor is executed.

See http://www.stroustrup.com/C++11FAQ.html#delegating-ctor and https://en.cppreference.com/w/cpp/language/initializer_list#Delegating_constructor for additional information.

R Sahu
  • 204,454
  • 14
  • 159
  • 270