user5880324
user5880324

Reputation:

C++ initialize member variables with { }

Recently I have seen member variable initialization in c++ as such:

class foo
{
public:
    foo();
private:
    bool bar{false};
};

What is the point of variable initialization like this/how does member variable declaration/definition like this differ or not differ from using an initialization list as such:

foo::foo() : bar(false) {}

Upvotes: 2

Views: 105

Answers (1)

IlBeldus
IlBeldus

Reputation: 1040

It's useful when you have a lot of constructors. bool bar{false}; means set bar to false unless the constructor initialises it to something else. it's just for convenience

See http://en.cppreference.com/w/cpp/language/data_members#Member_initialization for a more detailed explanation

Upvotes: 3

Related Questions