dimanche 8 octobre 2017

Will using a member initializer list make the initialization slightly faster?

In his book "Programming, Principles and practices using C++" Bjarne Stroustrup introduces the concept of member initializer list on pages 314-316 (§ 9.4.4). He uses the following example:

// Simple Date (year, month, day)

class Date
{
public:
    Date(int yy, int mm, int dd): y{yy}, m{mm}, d{dd}
    {
        //...
    }

private:
    int y, m, d;
};

On page 315 he says:

We could have written:

Date::Date(int yy, int mm, int dd)  // constructor
{
    y = yy;
    m = mm;
    d = dd;
}

but then we would in principle first have default initialized the members and then assigned values to them.

Therefore, can I conclude that using member initializer lists makes the code slightly faster? Of course, no one would notice on a modern PC. But I'm planning to use C++ for embedded development.

Aucun commentaire:

Enregistrer un commentaire