以下类层次结构表示抽象资源处理程序和资源层次结构。两者都有作为基类的接口。现在想象你编写一个系统,你可以在这些接口下实现多个特定的资源系统。这只是一个例子。特定的主类创建从东西派生的资源。现在,当创建的资源交给基本接口时,它将作为指向基本资源类的指针递交,但我想处理特定资源并访问其特定属性。C++使用镜像层次结构进行双重调度
我知道双重派遣,但我不认为它在这种情况下工作。我想阻止RTTI和dynamic_casts。你有处理这种情况的建议吗?
class resource;
class main_resource_handler
{
public:
virtual resource* create_resource() = 0;
virtual void do_some(resource* st) = 0;
};
class resource
{
};
class specific_resource : public resource
{
public:
int i;
};
class specific_resource_handler : public main_resource_handler
{
public:
stuff* create_resource) {
return new specific_resource);
}
void do_some(resource* st) {
// in here i want to work with specific resource
}
void do_some(specific_resource* st) {
// i want to get here
}
}
main_resource_handler* handler = new specific_resource_handler();
resource* res = handler->create_resource();
handler->do_some(res); /// here
有在回答这个问题的一些想法:http://stackoverflow.com/questions/696350/avoiding -parallel继承的层次结构 – quamrana 2010-02-28 18:52:03