简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM