initialization on construction

Started by
2 comments, last by skalco6 18 years, 4 months ago
In the end, once this has been compiled, are there any differences between those two :
// initializer list
class foo {
public:
    foo(int value) : bar(value) {}

private:
    int bar;
}

// explicit initialization
class foo {
public:
    foo(int value) {
        bar = value;
    }

private:
    int bar;
}
I'm wondering if it's just a matter of preferences or there is some point for using one or the other...
Advertisement
C++ FAQ Lite 10.6
For ints, the compiler will probably generate the same machine code. For class types, the first one is more efficient as the second one will generate a call to the default constructor followed by a call to the assignment operator (unless the compiler has access to the code for both and can optimize it out).
thx a lot guys

This topic is closed to new replies.

Advertisement