C++ Structs with Member Functions vs. Classes with Public Variables

前端 未结 6 569
感动是毒
感动是毒 2020-12-13 09:30

This is really a question of good form/best practices. I use structs in C++ to form objects that are designed to basically hold data, rather than making a class with a ton o

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-13 10:21

    I don't want to sparkle a holy war here; I usually differentiate it in this way:

    • For POD objects (i.e., data-only, without exposed behavior) declare the internals public and access them directly. Usage of struct keyword is convenient here and also serves as a hint of the object usage.
    • For non-POD objects declare the internals private and define public getters/setters. Usage of class keyword is more natural in these cases.

提交回复
热议问题