In this specific case, is there a difference betwe

2018-12-31 08:28发布

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...

11条回答
怪性笑人.
2楼-- · 2018-12-31 09:09

The real difference boils down to how the gcc compiler generate machine code and lay down the memory. Explain:

  • (phase1) Before the init body (including the init list): the compiler allocate required memory for the class. The class is alive already!
  • (phase2) In the init body: since the memory is allocated, every assignment now indicates an operation on the already exiting/'initialized' variable.

There are certainly other ways to handle const type members. But to ease their life, the gcc compiler writers decide to set up some rules

  1. const type members must be initialized before the init body.
  2. After phase1, any write operation only valid for non-constant members.
查看更多
后来的你喜欢了谁
3楼-- · 2018-12-31 09:12

Yes. In the first case you can declare _capacity, _data and _len as constants:

class MyClass
{
private:
    const int _capacity;
    const void *_data;
    const int _len;
// ...
};

This would be important if you want to ensure const-ness of these instance variables while computing their values at runtime, for example:

MyClass::MyClass() :
    _capacity(someMethod()),
    _data(someOtherMethod()),
    _len(yetAnotherMethod())
{
}

const instances must be initialized in the initializer list or the underlying types must provide public parameterless constructors (which primitive types do).

查看更多
明月照影归
4楼-- · 2018-12-31 09:13

I think this link http://www.cplusplus.com/forum/articles/17820/ gives an excellent explanation - especially for those new to C++.

The reason why intialiser lists are more efficient is that within the constructor body, only assignments take place, not initialisation. So if you are dealing with a non-built-in type, the default constructor for that object has already been called before the body of the constructor has been entered. Inside the constructor body, you are assigning a value to that object.

In effect, this is a call to the default constructor followed by a call to the copy-assignment operator. The initialiser list allows you to call the copy constructor directly, and this can sometimes be significantly faster (recall that the initialiser list is before the body of the constructor)

查看更多
听够珍惜
5楼-- · 2018-12-31 09:15

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.

查看更多
妖精总统
6楼-- · 2018-12-31 09:15

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.

查看更多
登录 后发表回答