C++11 - default member initialization

less than 1 minute read

You can initialize the member variable as shown the following in C++11. It will save a lot of typing when you have many constructors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
enum class BuildingType
{
    None,
    Resident,
    Commercial,
};
class Building
{
    BuildingType m_type { BuildingType::None };
    std::string m_address { "None" };
    int m_roomCount { 0 };
    std::string m_name;
    Building(std::string name)
     : m_name(std::move(name))
    {
    }
};