在这种情况下,使用类比使用结构有什么优势吗?(注意:它只会保存变量,永远不会有函数)
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.
这篇关于仅用于数据的类与结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何在 C++ 中读取和操作 CSV 文件数据?How can I read and manipulate CSV file data in C++?(如何在 C++ 中读取和操作 CSV 文件数据?)
在 C++ 中,为什么我不能像这样编写 for() 循环:In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,为什么我不能像这样编写 for() 循环: for(
OpenMP 如何处理嵌套循环?How does OpenMP handle nested loops?(OpenMP 如何处理嵌套循环?)
在循环 C++ 中重用线程Reusing thread in loop c++(在循环 C++ 中重用线程)
需要精确的线程睡眠.最大 1ms 误差Precise thread sleep needed. Max 1ms error(需要精确的线程睡眠.最大 1ms 误差)
是否需要“do {...} while ()"?环形?Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?环形?)