initialization lists

Mar 9, 2011 at 7:44am
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/initialization-lists-c++.html

but couldn't understand :(
Last edited on Mar 9, 2011 at 7:52am
Mar 9, 2011 at 7:56am
i don't have any information about copy constructor also.i think it is related to it too.
Mar 9, 2011 at 8:15am
An initialization list is used in the constructor to initialize class members and base classes
eg:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
    }
}
Mar 9, 2011 at 9:42am
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
}
};


Base ( int y, int z ) : x ( y+z ) is x a variable or a construction?

and what does "but it calls the constructor for x instead of the assignment operator" mean?

and can you explain the second code with examples.Moreover i have a little information about copy constructors.Could you explain it.Thanks for all your helps:
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
}
}
Mar 9, 2011 at 9:52am
x is the class member.

Here is an example showing constructors/copy-constructors and assignment operator:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;

class C
{
    int i;
public:
    C() : i ( 0 ) { cout << "Default constructor" << endl; }
    C ( int j ) : i ( j ) { cout << "Constructor with value = " << i << endl; }
    C ( const C & other ) : i ( other.i ) { cout << "Copy-constructor " << other.i << endl; }
    C& operator= ( const C& other )
    {
        i = other.i;
        cout << "Assignment operator " << other.i << endl;
    }
    C& operator= ( int j )
    {
        i = j;
        cout << "Assignment operator with value = " << j << endl;
    }
    ~C() { cout << "Destructor" << endl; }
};

struct Base
{
    C x;
    Base ( int y, int z ) : x ( y+z ) 
    {
    }

    Base ( int y )
    {
        x = y;
    }
};

int main()
{
     Base a ( 1,2 ); // calls constructor with initializer list, which calls C constructor
     Base b ( 3 ); // calls constructor without initializer list, which calls C assignment operator
}

Try to modify the code above and see if you can understand how these things work
Mar 9, 2011 at 10:33am
closed account (S6k9GNh0)
An initialization list can also be referring to initializing elements in an array like so:

int bob[3] = { 1, 2, 3 };

For clarification, note that you are referring to ctor initialization.
Last edited on Mar 9, 2011 at 10:35am
Topic archived. No new replies allowed.