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

后端 未结 12 837
北恋
北恋 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:27

    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.

    0 讨论(0)
  • 2020-11-22 08:30

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

    0 讨论(0)
  • 2020-11-22 08:30

    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.

    0 讨论(0)
  • 2020-11-22 08:31

    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.

    0 讨论(0)
  • 2020-11-22 08:36

    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.

    0 讨论(0)
  • 2020-11-22 08:39

    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.

    0 讨论(0)
提交回复
热议问题