// 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;
}
initialization on construction
In the end, once this has been compiled, are there any differences between those two :
I'm wondering if it's just a matter of preferences or there is some point for using one or the other...
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).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement