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 08:49

A big difference is that the assignment can initialize members of a parent class; the initializer only works on members declared at the current class scope.

查看更多
与风俱净
3楼-- · 2018-12-31 08:54

If you write an initializer list, you do all in one step; if you don't write an initilizer list, you'll do 2 steps: one for declaration and one for asign the value.

查看更多
与君花间醉酒
4楼-- · 2018-12-31 08:55

There is only one way to initialize base class instances and non-static member variables and that is using the initializer list.

If you don't specify a base or non-static member variable in your constructor's initializer list then that member or base will either be default-initialized (if the member/base is a non-POD class type or array of non-POD class types) or left uninitialized otherwise.

Once the constructor body is entered, all bases or members will have been initialized or left uninitialized (i.e. they will have an indeterminate value). There is no opportunity in the constructor body to influence how they should be initialized.

You may be able to assign new values to members in the constructor body but it is not possible to assign to const members or members of class type which have been made non-assignable and it is not possible to rebind references.

For built in types and some user-defined types, assigning in the constructor body may have exactly the same effect as initializing with the same value in the initializer list.

If you fail to name a member or base in an initializer list and that entity is a reference, has class type with no accessible user-declared default constructor, is const qualified and has POD type or is a POD class type or array of POD class type containing a const qualified member (directly or indirectly) then the program is ill-formed.

查看更多
何处买醉
5楼-- · 2018-12-31 09:08

You need to use initialization list to initialize constant members,references and base class

When you need to initialize constant member, references and pass parameters to base class constructors, as mentioned in comments, you need to use initialization list.

struct aa
{
    int i;
    const int ci;       // constant member

    aa() : i(0) {} // will fail, constant member not initialized
};

struct aa
{
    int i;
    const int ci;

    aa() : i(0) { ci = 3;} // will fail, ci is constant
};

struct aa
{
    int i;
    const int ci;

    aa() : i(0), ci(3) {} // works
};

Example (non exhaustive) class/struct contains reference:

struct bb {};

struct aa
{
    bb& rb;
    aa(bb& b ) : rb(b) {}
};

// usage:

bb b;
aa a(b);

And example of initializing base class that requires a parameter (e.g. no default constructor):

struct bb {};

struct dd
{
    char c;
    dd(char x) : c(x) {}
};

struct aa : dd
{
    bb& rb;
    aa(bb& b ) : dd('a'), rb(b) {}
};
查看更多
余生无你
6楼-- · 2018-12-31 09:08

I'll add that if you have members of class type with no default constructor available, initialization is the only way to construct your class.

查看更多
高级女魔头
7楼-- · 2018-12-31 09:08

There is a difference between initialization list and initialization statement in a constructor. Let's consider below code:

#include <initializer_list>
#include <iostream>
#include <algorithm>
#include <numeric>

class MyBase {
public:
    MyBase() {
        std::cout << __FUNCTION__ << std::endl;
    }
};

class MyClass : public MyBase {
public:
    MyClass::MyClass() : _capacity( 15 ), _data( NULL ), _len( 0 ) {
        std::cout << __FUNCTION__ << std::endl;
    }
private:
    int _capacity;
    int* _data;
    int _len;
};

class MyClass2 : public MyBase {
public:
    MyClass2::MyClass2() {
        std::cout << __FUNCTION__ << std::endl;
        _capacity = 15;
        _data = NULL;
        _len = 0;
    }
private:
    int _capacity;
    int* _data;
    int _len;
};

int main() {
    MyClass c;
    MyClass2 d;

    return 0;
}

When MyClass is used, all the members will be initialized before the first statement in a constructor executed.

But, when MyClass2 is used, all the members are not initialized when the first statement in a constructor executed.

In later case, there may be regression problem when someone added some code in a constructor before a certain member is initialized.

查看更多
登录 后发表回答