How much functionality is “acceptable” for a C++ struct?

后端 未结 19 2317
礼貌的吻别
礼貌的吻别 2021-02-07 09:31

My first post so please go easy on me!

I know that there\'s no real difference between structs and classes in C++, but a lot of people including me use a struct or class

19条回答
  •  既然无缘
    2021-02-07 10:00

    I'll add a very few methods to a struct, as a matter of convenience, only if I'm actually using them. Of course all of them are public. If I need any more than this, it immediately gets converted to a class.

    1. A default constructor, to initialize the data members to known values.
    2. A constructor with one parameter for each member.
    3. operator<, for easy inclusion in sets and maps.
    4. operator==.

提交回复
热议问题