【发布时间】:2013-06-22 20:55:04
【问题描述】:
在我的 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};
};
};
我该如何解决我的问题?我的结构应该是什么?
【问题讨论】:
-
只是出于好奇:使用“无效退货”的充分理由是什么? :)
-
当然,我的代码不是这个。我需要的结构是接受输入作为参考,在内部进行更新,这就是原因。
-
好的,这很有道理。 :)
标签: c++ oop architecture object-oriented-analysis