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

后端 未结 12 1163
北恋
北恋 2020-11-22 08:16

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

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

12条回答
  •  广开言路
    2020-11-22 08:50

    Depends on the types involved. The difference is similar between

    std::string a;
    a = "hai";
    

    and

    std::string a("hai");
    

    where the second form is initialization list- that is, it makes a difference if the type requires constructor arguments or is more efficient with constructor arguments.

提交回复
热议问题