以下类层次结构表示抽象资源处理程序和资源层次结构。两者都将接口作为基类。现在想象你编写一个系统,你可以在这些接口下实现多个特定的资源系统。这里只是一个例子。特定的主类创建从 stuff 派生的资源。现在,当创建的资源被传递给基本接口时,它作为指向基本资源类的指针传递,但我想处理特定资源并访问其特定属性。
我知道双重调度,但我认为它在这种情况下不起作用。我想阻止 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