Is there a difference between how these are initialized ? Is one more favorable than the other ?
class Foo{
public:
Foo();
private:
int x_;
};
Foo::Foo() {
this->x_ = 5;
}
Foo::Foo()
: x_(5)
{}
Is there a difference between how these are initialized ? Is one more favorable than the other ?
class Foo{
public:
Foo();
private:
int x_;
};
Foo::Foo() {
this->x_ = 5;
}
Foo::Foo()
: x_(5)
{}
This is member initialisation and is generally preferable.
Foo::Foo()
: x_(5)
{}
More in-depth answers are here: Why should I prefer to use member initialization list?