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

后端 未结 19 2328
礼貌的吻别
礼貌的吻别 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:12

    I think there are three major, coherent schools of thought:

    1. People that don't care either way and use struct and class interchangeably.
    2. People that use structs only to represent small POD.
    3. People who use structs as records.

    I can't make a conclusive argument for either of these strategies. I tend to follow path 2 but I also use structs for non-POD types when I see it fitting, especially for function objects (even if these may not fulfil POD requirements).

    (Incidentally, the C++ FAQ lite has a pretty good definition of POD).

    EDIT I didn't touch template metaprogramming techniques, such as using struct for placeholders (type tags) or to implement metafunctions. I guess there's absolutely no controversy in these cases: since they never contain methods (or even data), always use struct).

提交回复
热议问题