Search code examples
c++initializationinitialization-list

In this specific case, is there a difference between using a member initializer list and assigning values in a constructor?


Internally and about the generated code, is there a really difference between :

MyClass::MyClass(): _capacity(15), _data(NULL), _len(0)
{
}

and

MyClass::MyClass()
{
  _capacity=15;
  _data=NULL;
  _len=0
}

thanks...


Solution

  • Assuming that those values are primitive types, then no, there's no difference. Initialization lists only make a difference when you have objects as members, since instead of using default initialization followed by assignment, the initialization list lets you initialize the object to its final value. This can actually be noticeably faster.