Bjarne Stroustrup: My rule of thumb is that you should have a real class with an interface and a hidden representation if and only if you can consider an invariant for the class.
在当前项目中,我有一个名为 Widget 的基类。它有 x、y、宽度、高度的私有(private)变量(基本上是一个矩形数据结构)和每个变量的公共(public) getter 和 setter。除了作为一个愚蠢的变量持有者之外,他们对类(class)没有任何意义。根据上面 Bjarnes 的评论,我摆脱了这个类,但我想知道我应该如何与需要它们的子类共享这个数据结构。我应该单独将他们作为每个类(class)的成员吗?将它们放在 Widget namespace 中?
最佳答案
你可以使用一个结构
例如
struct widget
{
int x;
int y;
int w;
int h;
};
关于c++ - 我应该如何共享数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13326718/