initialization lists vs explicit assignment

I read the following:

"C++ provides another way of initializing member variables that allows us to initialize member variables when they are created rather than afterwards. This is done through use of an initialization list."

However, these two examples below show that the variables are initialized AFTER they are created, whether we use explicit initialization in body of constructor, or use an initialization list. By the time the variabels are initialized, they were already created in the private section:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Something
{
private:
    int m_nValue;
    double m_dValue;
    int *m_pnValue;
 
public:
    Something()
    {
        m_nValue = 0;
        m_dValue = 0.0;
        m_pnValue = 0;
    }
};


1
2
3
4
5
6
7
8
9
10
11
12
class Something
{
private:
    int m_nValue;
    double m_dValue;
    int *m_pnValue;
 
public:
    Something() : m_nValue(0), m_dValue(0.0), m_pnValue(0)
    {
    }
};


So what does it mean that the initialization list allows us to initialize member variables when they are created rather than afterwards?
http://www.parashift.com/c++-faq/init-lists.html

So what does it mean that the initialization list allows us to initialize member variables when they are created rather than afterwards?

It means that for objects (not "plain" variables like int's and double's in your example) their own constructor is called directly, in order to construct them with the desired value.

This is opposed to using assignment in the constructor of the Something class.
In that case, a temporary object may be created (constructor function call), then it would be assigned to the object (assignment operator function call), wasting time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Something
{
private:
    Thing t;

public:
    Something(): t(0) // call constructor of Thing
    {
    }

    // versus

    Something()
    {
        t = Thing(0); // temporary object and Thing::operator=
    }
};


It is a good habit to use initialization lists in constructors, despite there being cases where they don't improve performance (such as in your original example).
> these two examples below show that the variables are initialized AFTER they are created,
¿how are you measuring that?
OP is confused by terminology.

All member variables exist in memory before they are finished being "created". That is, there are two parts to "creating" an object:
(1) obtaining memory for it
(2) initializing the memory for it

In every example, including your original ones, the objects exist in memory before the constructor is ever called. Why? Because memory had to be reserved for use by a Something before you can start initializing that memory.

Once you have memory, then you can initialize it.


The next misunderstanding you have is what object is being initialized.

As Catfish4 indicated, when you "create" an object, it might be necessary (or convenient) to create a temporary object somewhere, initialize it, and then directly copy (or copy construct) the object you are trying to "create" from the temporary.

Likewise, when you have a constructor that assigns values to the member fields, the issue is compounded.

An initializer list allows both of those problems to be eliminated, by providing the object's initializer code a specific, final spot in memory to initialize.

In your example, the object's memory is obtained, as always, with random data in it. Because the object's fields are simple data types, both the constructor and the initializer list need do nothing but set the fields to the appropriate values for "zero" (which may or may not actually be a bunch of zeroed bytes -- which is why we need initialization to begin with, since the underlying meaning of zero might not actually be a zero).

Hope this helps.
However, these two examples below show that the variables are initialized AFTER they are created, whether we use explicit initialization in body of constructor, or use an initialization list. By the time the variabels are initialized, they were already created in the private section:

Those two examples show nothing of the sort.

1
2
3
4
5
6
7
8
9
10
11
12
class Something
{
private:
    int m_nValue;
    double m_dValue;
    int *m_pnValue;
 
public:
    Something() : m_nValue(0), m_dValue(0.0), m_pnValue(0)
    {
    }
};
and
1
2
3
4
5
6
7
8
9
10
11
12
class Something
{
public:
    Something() : m_nValue(0), m_dValue(0.0), m_pnValue(0)
    {
    }

private:
    int m_nValue;
    double m_dValue;
    int *m_pnValue;
};
are equivalent.

Where the variables occur in the class doesn't have anything to do with their initialization. That shouldn't be confused with the order of initialization though, because that is affected by the order the variables are introduced in the class.
@cire: ¿what code are you reading?
Topic archived. No new replies allowed.