在我的 C++ 程序中,我想创建一个具有宽度、高度、面积等属性的对象。我还想声明使用和更新这些属性的方法。
我想要在名为 WidthManipulator 的标头、命名空间或子类(无论如何都是可能的)中以某种方式列出的属性“宽度”的“设置”和“获取”方法。
我想以这种方式创建结构的原因是我想为另一个类的另一个方法使用“get”名称,例如 HeightManipulator。
但是对于嵌套类,我得到 Rectangle::WidthManipulator::Get() 的“非法调用非静态成员函数”错误。我也不想创建 Manipulator 对象,因为这些类没有属性,只是使用和更新父属性的方法......还有一件事,我想使用 void 返回是出于我自己的一个很好的理由。
class Rectangle{
public:
int width,height;
int area;
int widthreturned;
class WidthManipulator{
public:
void Set(int x){width = x;}
void Get(){widthreturned = width};
};
};
我该如何解决我的问题?我的结构应该是什么?