C++11 – default member initialization

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.

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

Advertisements
This entry was posted in C++, Programming and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s