仅用于数据的类与结构? [英] Class vs Struct for data only?

查看:29
本文介绍了仅用于数据的类与结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种情况下,使用类比使用结构有什么优势吗?(注意:它只会保存变量,永远不会有函数)

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:

推荐答案

使用一个并没有真正的优势,在 C++ 中,结构和类之间的唯一区别是其成员的默认可见性(结构默认为public,类默认为private).

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 在评论中提出了一个很好的观点,所以我要在这里引用他:

litb made a good point in the comment so I'm going to quote him here:

另一个重要的区别是结构派生自其他默认情况下,类/结构公共,而类通过私有派生默认.

one important other difference is that structs derive from other classes/struct public by default, while classes derive privately by default.

这篇关于仅用于数据的类与结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆