When should you use a class vs a struct in C++?

后端 未结 25 2119
误落风尘
误落风尘 2020-11-22 00:18

In what scenarios is it better to use a struct vs a class in C++?

25条回答
  •  执笔经年
    2020-11-22 00:47

    I never use "struct" in C++.

    I can't ever imagine a scenario where you would use a struct when you want private members, unless you're willfully trying to be confusing.

    It seems that using structs is more of a syntactic indication of how the data will be used, but I'd rather just make a class and try to make that explicit in the name of the class, or through comments.

    E.g.

    class PublicInputData {
        //data members
     };
    

提交回复
热议问题