0

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)
{}

1 Answers1

0

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?

Community
  • 1
  • 1
Jack Deeth
  • 3,062
  • 3
  • 24
  • 39