加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – Class vs Struct仅供数据使用?

发布时间:2020-12-16 05:37:01 所属栏目:百科 来源:网络整理
导读:在这样的情况下,使用类超过结构体有什么优势吗? (注意:它只会保存变量,永远不会有函数) class Foo { private: struct Pos { int x,y,z };public: Pos Position; }; 与: struct Foo { struct Pos { int x,z } Pos;}; 类似的问题: When should you use a c
在这样的情况下,使用类超过结构体有什么优势吗?
(注意:它只会保存变量,永远不会有函数)
class Foo { 
private:   
   struct Pos { int x,y,z };
public:    
   Pos Position; 
};

与:

struct Foo {
   struct Pos { int x,z } Pos;
};

类似的问题:

> 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中,没有真正的优势,在c中,struct和类之间的唯一区别是它的成员的默认可见性(structs默认为public,class默认为private).

就个人而言,我倾向于倾向于使用POD类型的结构体,并将其他类使用类.

编辑:litb在评论中提出了一个好点,所以我要在这里引用他:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读