What are the differences between struct and class in C++?

后端 未结 30 3255
盖世英雄少女心
盖世英雄少女心 2020-11-21 05:38

This question was already asked in the context of C#/.Net.

Now I\'d like to learn the differences between a struct and a class in C++. Please discuss the technical d

30条回答
  •  醉酒成梦
    2020-11-21 06:09

    1. Member of a class defined with the keyword class are private by default. Members of a class defined with the keywords struct (or union) are public by default.

    2. In absence of an access-specifier for a base class, public is assumed when the derived class is declared struct and private is assumed when the class is declared class.

    3. You can declare an enum class but not an enum struct.

    4. You can use template but not template.

    Note also that the C++ standard allows you to forward-declare a type as a struct, and then use class when declaring the type and vice-versa. Also, std::is_class::value is true for Y being a struct and a class, but is false for an enum class.

提交回复
热议问题