mdmiller002 mdmiller002 - 1 year ago 57
C++ Question

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

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download