以下類層次結構表示抽象資源處理程序和資源層次結構。兩者都有作爲基類的接口。現在想象你編寫一個系統,你可以在這些接口下實現多個特定的資源系統。這只是一個例子。特定的主類創建從東西派生的資源。現在,當創建的資源交給基本接口時,它將作爲指向基本資源類的指針遞交,但我想處理特定資源並訪問其特定屬性。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