Class和Struct仅用于数据? [英] Class vs Struct for data only?

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

问题描述

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

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;
};


  • http://stackoverflow.com/questions/54585/when-should-you-use-a-class-vs-a-struct-in-c
  • http://stackoverflow.com/questions/92859/what-are-the-differences-between-struct-and-class-in-c
  • http://stackoverflow.com/questions/85553/when-should-i-use-a-struct-instead-of-a-class

推荐答案

在c ++中没有真正的优点,在c ++中,结构和类之间的唯一区别是它的成员的默认可见性(structs默认为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:


一个重要的其他区别是
structs派生自其他
类/ struct public默认情况下,
,而类通过
默认。 >

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

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

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