initialization lists
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: initialization lists

  1. #1
    Join Date
    Mar 2011
    Location
    Washington D.C.
    Posts
    41

    initialization lists

    i have searched many sources about initialization lists but i can't find a useful source . can't understand what it is and for what it is used .Can Anybody explain it in details or show me a good source? i read from http://www.cprogramming.com/tutorial...lists-c++.html

    but couldn't understand

    moreover can you explain that code step by step with all details(i dont know copy constructors and assignment operators)..Please don't pass any point .Thanks.

    struct Base
    {
    int x;
    Base ( int y, int z ) : x ( y+z )
    {
    // list above is the same as x=y+z; but it calls the constructor for x instead of the assignment operator
    }
    };

    struct Derived : public Base
    {
    double d;
    Derived ( int y, double d ) : Base ( x, 5 ), d ( d )
    {
    // Above calls Base constructor and initializes d
    // Notice that the d outside parentheses is the struct member,
    // the one inside parentheses is the constructor parameter
    }
    }

  2. #2
    Join Date
    Apr 2008
    Posts
    726

    Re: initialization lists

    Is English not your first language? The comments pretty much say it all.

    initialiser list is for constructing members.

    use some code like you have above, compiler it, and test it in a debugger - step through line by line and you can see what is happening.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: initialization lists

    The comments in that code block seem to be describing what's going on pretty well, although I do abhor the choice to use the same name (d) for both a member and constructor parameter. What aspect are you having trouble with?

  4. #4
    Join Date
    Mar 2011
    Location
    Washington D.C.
    Posts
    41

    Re: initialization lists

    in that row:
    // list above is the same as x=y+z; but it calls the constructor for x instead of the assignment operator


    list above is the same as x=y+z. > That's OK.

    BUT didn't understand what does " but it calls the constructor for x instead of the assignment " mean? how many process exist here? ?What happens step by step?

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: initialization lists

    Quote Originally Posted by loves_oi View Post
    BUT didn't understand what does " but it calls the constructor for x instead of the assignment " mean? how many process exist here? ?What happens step by step?
    Well, the wording would be more appropriate if x were a class type rather than a primitive (int). However, the general idea is that there's a distinction between construction of a variable and assignment to a variable.

    This constructs an int with a specified value; the variable never exists with any other value:
    Code:
    int val(5);
    This assigns a different value to a pre-existing variable:
    Code:
    val = 7;
    Note, for completeness, I should point out that this is actually initialization rather than assignment:
    Code:
    int val = 9;
    When a class constructor has an initializer list, you control how the class members in the list are initialized. When it doesn't, all members are first default-initialized. Maybe you assign something else to them within the constructor body, but that's assignment, not initialization.

    For primitives the difference is negligible. For some class types it may not be; and for const members or reference members, initialization is mandatory because assignment to them is impossible.

  6. #6
    Join Date
    May 2009
    Posts
    2,413

    Re: initialization lists

    Quote Originally Posted by loves_oi View Post
    BUT didn't understand what does " but it calls the constructor for x instead of the assignment " mean? how many process exist here? ?What happens step by step?
    It's nothing to worry about really.

    You can assign initial values to the object variables either in the constructor body or in the initialization list. The latter is marginally more efficient so it's preferred.
    Last edited by nuzzle; March 9th, 2011 at 11:30 AM.

  7. #7
    Join Date
    Mar 2011
    Location
    Washington D.C.
    Posts
    41

    Re: initialization lists

    Quote Originally Posted by Lindley View Post
    Well, the wording would be more appropriate if x were a class type rather than a primitive (int). However, the general idea is that there's a distinction between construction of a variable and assignment to a variable.

    This constructs an int with a specified value; the variable never exists with any other value:
    Code:
    int val(5);
    This assigns a different value to a pre-existing variable:
    Code:
    val = 7;
    Note, for completeness, I should point out that this is actually initialization rather than assignment:
    Code:
    int val = 9;
    When a class constructor has an initializer list, you control how the class members in the list are initialized. When it doesn't, all members are first default-initialized. Maybe you assign something else to them within the constructor body, but that's assignment, not initialization.

    For primitives the difference is negligible. For some class types it may not be; and for const members or reference members, initialization is mandatory because assignment to them is impossible.
    when x is primitive(int) ,it's OK.How about it would be class type?How will it work?

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: initialization lists

    Here's the simplest way to demonstrate the difference (probably). Consider this class:

    Code:
    Class MyClass
    {
        int value;
    public:
        MyClass()
            : value(0)
        {
            cout << "Default constructor. (Setting value to 0)\n";
        }
        MyClass(int value_)
            : value(value_)
        {
            cout << "Int constructor (" << value_ << ").\n";
        }
        MyClass& operator=(int rhs)
        {
            value = rhs;
            cout << "Assignment operator (" << rhs << ")\n";
        }
    };
    
    class MyInitializerListClass
    {
        MyClass x;
    public:
        MyInitializerListClass()
            : x(9)
        {}
    };
    
    class MyConstructorAssignmentClass
    {
        MyClass x;
    public:
        MyConstructorAssignmentClass()
        {
            x = 9;
        }
    };
    
    int main()
    {
        cout << "Constructing with initializer list:\n";
        MyInitializerListClass obj1;
        cout << "Constructing with assignment:\n";
        MyConstructorAssignmentClass obj2;
    }
    I say this will probably demonstrate the issue because compiler optimizations can do funny things to constructors and assignment operators. So to really understand the logic, compile with optimizations disabled (Debug mode in Visual Studio).

    Here we are replacing "int x" with "MyClass x" and demonstrating both forms of constructor.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center