问题描述
在这样的情况下,使用一个类比结构有什么优势吗?
(注意:它只保存变量,不会有函数)
Is there any advantage over using a class over a struct in cases such as these?(note: it will only hold variables, there will never be functions)
class Foo {
private:
struct Pos { int x, y, z };
public:
Pos Position;
};
与
struct Foo {
struct Pos { int x, y, z } Pos;
};
类似问题:
Similar questions:
- When should you use a class vs a struct in C++?
- What are the differences between struct and class in C++?
- When should I use a struct instead of a class?
推荐答案
在c ++中使用一个没有真正的优势,结构体和类之间的默认可见性是其成员的默认可见性(结构体默认为public,类默认为私有)。
There is no real advantage of using one over the other, in c++, the only difference between a struct and a class is the default visibility of it's members (structs default to public, classes default to private).
我个人倾向于喜欢POD类型并使用其他类别的课程。
Personally, I tend to prefer structs for POD types and use classes for everything else.
编辑:评论中的一个好点,所以我要在这里引用他:
litb made a good point in the comment so I'm going to quote him here:
这篇关于Class和Struct仅用于数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!