为什么我什至应该考虑在C ++中使用结构? [英] Why should I even consider using structs in C++?

查看:51
本文介绍了为什么我什至应该考虑在C ++中使用结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中声明结构是否有优势?为什么我不应该只创建一个仅包含数据成员的类(即没有方法)?

Is there ever an advantage of declaring a struct in C++? Why shouldn't I just make a class consisting only of data members(i.e. no methods)?

谢谢

推荐答案

当您拥有POD类型时,公开是节省一行...

When you have a POD type where everything is public is saves a line...

struct Color {
    int r;
    int g;
    int b;
};

vs

class Color {
public:
    int r;
    int g;
    int b;
};

对于那些只是笨拙的事物的对象,这也是常见的做法。愚蠢的意思是没有构造函数,运算符,方法。

And it's also common practice for objects which are just dumb containers of things. Dumb meaning no constructors, operators, methods.

这篇关于为什么我什至应该考虑在C ++中使用结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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