如何递归访问不同的类?
我想要一组可互换的类来对数据集执行操作。我“认为”我想做的就是所谓的多态性,但我不确定。
这是我正在尝试做的一个例子。我想创建一个包含初始值的对象,然后初始化另一个类以使用第一个类中的数据并使用 exec 执行操作,然后使用另一个递归类重复此操作。
我希望能够改变操作的顺序,这个想法是任何类都可以调用 exec() ,它总是返回 unsigned long 。 init() 可能不同,但在初始化期间调用,并且不会在递归类中访问。
谢谢,
class operationsObject {
public:
virtual unsigned long exec (void) =0;
};
class addObject: public operationsObject {
private:
unsigned long valueA, valueB;
public:
void init(unsigned long a, unsigned long b)
{valueA = a; valueB = b;}
unsigned long exec()
{return valueA + valueB;}
};
class subtractObject: public operationsObject {
private:
operationsObject *obj;
unsigned long valueA;
public:
void init(unsigned long a, operationsObject *o)
{valueA = a; obj = o;}
unsigned long exec()
{return obj->exec() - valueA;}
};
class multiplyObject: public operationsObject {
private:
operationsObject *obj;
unsigned long valueA, valueB;
public:
void init(unsigned long a, unsigned long b, operationsObject *o)
{valueA = a; valueB = b; obj = o;}
unsigned long exec()
{return obj->exec() * valueA * valueB;}
};
int main(){
operationsObject *op1 = new addObject;
operationsObject *op2 = new subtractObject;
operationsObject *op3 = new multiplyObject;
op1->init(4,5);
op2->init(4, op1);
op3->init(1, 2, op2);
unsigned retVal = op3->exec();
}
I want to have a set of interchangeable classes to perform operations on a dataset. I "think" what I'm trying to do is called Polymorphism but I'm unsure.
This is an example of what I'm trying to do. I want to create an object which contains the initial values, then initialize another class to use the data in the first class and perform a operation using exec, then repeat this with another recursive class.
I want to be able to change the order of operations, the idea is any class can call exec() which will always return unsigned long. init() might be different but is called during initialization and won't be accessed within a recursive class.
Thanks,
class operationsObject {
public:
virtual unsigned long exec (void) =0;
};
class addObject: public operationsObject {
private:
unsigned long valueA, valueB;
public:
void init(unsigned long a, unsigned long b)
{valueA = a; valueB = b;}
unsigned long exec()
{return valueA + valueB;}
};
class subtractObject: public operationsObject {
private:
operationsObject *obj;
unsigned long valueA;
public:
void init(unsigned long a, operationsObject *o)
{valueA = a; obj = o;}
unsigned long exec()
{return obj->exec() - valueA;}
};
class multiplyObject: public operationsObject {
private:
operationsObject *obj;
unsigned long valueA, valueB;
public:
void init(unsigned long a, unsigned long b, operationsObject *o)
{valueA = a; valueB = b; obj = o;}
unsigned long exec()
{return obj->exec() * valueA * valueB;}
};
int main(){
operationsObject *op1 = new addObject;
operationsObject *op2 = new subtractObject;
operationsObject *op3 = new multiplyObject;
op1->init(4,5);
op2->init(4, op1);
op3->init(1, 2, op2);
unsigned retVal = op3->exec();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的描述听起来像是您可能想要检查以下设计模式:责任链 。
我可能会想出这样的东西:
exec
(非虚拟!),调用execLocal
实现您正在寻找的行为。现在,
exec
可以定义为:Your description sounds like you might want to check the following design pattern: Chain of Responsibility.
I'd probably come up with something like this:
exec
(non-virtual!) that calls aexecLocal
that implements the behavior you are looking for.Now,
exec
could be defined as: