c++ - 我应该如何共享数据结构

标签 c++ oop

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/

相关文章:

c - C中的面向对象编程

r - 内存中没有变量名的数据仍然是 R 中的对象吗?

c++ - 本地类变量位置

javascript - 如何获取父对象的函数(NodeJS模块)

C++:如何创建一个接受连接字符串作为参数的函数?

c++ - 如何改进此代码以便多线程运行?

c++ - C++ 中的非线性编程库

c++ - typeid() 面向对象的设计替代方案

c++ - 功能尝试 block 。一个有趣的例子

c++ - 在 WinAPI 函数中使用 std::wstring:并非所有文本都已发送