9

我有一个关于 C++ 双重调度的问题。在下面的代码中,我希望第二组的结果与第一组的结果相匹配。

我不知道实际类型(除非我尝试 dynamic_cast),但我知道该对象继承自 BaseClass 类型。实现这一目标的最有效(性能方面)的方法是什么?

在谷歌搜索了一段时间后,我发现了双重调度和 loki 多方法。我在 Shape 示例中遇到的问题是,在我的应用程序中,Processor 和 BaseClass 是完全独立的,并且没有可以相互调用的通用方法。其次,只有一个处理器(即没有任何东西继承自它)。

谢谢你的帮助。

#include <iostream>
#include <string>
using namespace std;

class BaseClass{
public:
       BaseClass(){}
       virtual void myFunction(){cout << "base myFunction called" << endl;}
};

class Derived1: public BaseClass{
public:
       Derived1():BaseClass(){}
       void myFunction(){cout << "Derived1 myFunction called" << endl;}
};


class Derived2: public BaseClass{
public:
       Derived2():BaseClass(){}
       void myFunction(){cout << "Derived2 myFunction called" << endl;}
};

class Derived3: public BaseClass{
public:
       Derived3():BaseClass(){}
       void myFunction(){cout << "Derived3 myFunction called" << endl;}

};

class Processor{
public:
       Processor(){}
       virtual void processObj(BaseClass* bc){cout << "got a base object" << endl; bc->myFunction();}
       virtual void processObj(Derived1* d1){cout << "got a derived1 object" << endl; d1->myFunction();}
       virtual void processObj(Derived2* d2){cout << "got a derived2 object" << endl; d2->myFunction(); }
};


int main() {
   BaseClass *bcp=new BaseClass();
   Derived1 *dc1p=new Derived1();   
   Derived2 *dc2p=new Derived2();
   Derived3 *dc3p=new Derived3();

   Processor p;//can also use Processor* p = new Processor()

   //first set results
   p.processObj(bcp);
   p.processObj(dc1p);
   p.processObj(dc2p);
   p.processObj(dc3p);

   BaseClass *bcp1=bcp;
   BaseClass *dc1p1=dc1p;   
   BaseClass *dc2p1=dc2p;
   BaseClass *dc3p1=dc3p;

   //second set results
   p.processObj(bcp1);
   p.processObj(dc1p1);
   p.processObj(dc2p1);
   p.processObj(dc3p1);

   return 0;
}
4

4 回答 4

10

您错过了双重调度的“双重”部分。

这种模式的重点是确保调用处理器的正确方法——接受正确类型的方法。由于处理器最初不知道传递给它的对象的类型,因此您需要对象告诉处理器它的类型是什么。

本质上,每个对象都需要一个虚processMe(Processor &p)方法,处理器调用它。processMe调用的执行p.processObject(this)。但这一次,“this”有一个已知类型!因此,您最终得到的不是无限递归,而是proceessObject被称为

于 2009-01-09T21:53:41.950 回答
6

访问者模式就是为处理这种情况而设计的。

于 2009-01-09T21:48:58.340 回答
4

您必须在 BaseClass 上放置一个虚拟方法才能从派生类调用 processObj。

class BaseClass{
public:
       BaseClass(){}
       virtual void ProcessThis(Processor &p) { p.processObj(this); }
       virtual void myFunction(){cout << "base myFunction called" << endl;}
};

class Derived1: public BaseClass{
public:
       Derived1():BaseClass(){}
       void ProcessThis(Processor &p) { p.processObj(this); }
       void myFunction(){cout << "Derived1 myFunction called" << endl;}
};

class Derived2: public BaseClass{
public:
       Derived2():BaseClass(){}
       void ProcessThis(Processor &p) { p.processObj(this); }
       void myFunction(){cout << "Derived2 myFunction called" << endl;}
};

class Derived3: public BaseClass{
public:
       Derived3():BaseClass(){}
       void ProcessThis(Processor &p) { p.processObj(this); }
       void myFunction(){cout << "Derived3 myFunction called" << endl;}

};

class Processor{
public:
       Processor(){}
       virtual void processObj(BaseClass* bc){cout << "got a base object" << endl; bc->myFunction();}
       virtual void processObj(Derived1* d1){cout << "got a derived1 object" << endl; d1->myFunction();}
       virtual void processObj(Derived2* d2){cout << "got a derived2 object" << endl; d2->myFunction(); }
};

int main() {
   BaseClass *bcp=new BaseClass();
   Derived1 *dc1p=new Derived1();   
   Derived2 *dc2p=new Derived2();
   Derived3 *dc3p=new Derived3();

   Processor p;//can also use Processor* p = new Processor()

   //first set results
   bcp->ProcessThis(p);
   dc1p->ProcessThis(p);
   dc1p->ProcessThis(p);
   dc3p->ProcessThis(p);

   BaseClass *bcp1=bcp;
   BaseClass *dc1p1=dc1p;   
   BaseClass *dc2p1=dc2p;
   BaseClass *dc3p1=dc3p;

   //second set results
   bcp1->ProcessThis(p);
   dc1p1->ProcessThis(p);
   dc2p1->ProcessThis(p);
   dc3p1->ProcessThis(p);

   Processor p2;
   bcp1->ProcessThis(p2);
   dc1p1->ProcessThis(p2);
   dc2p1->ProcessThis(p2);
   dc3p1->ProcessThis(p2);

   return 0;
}

您基本上想要访问者模式,但只有一种类型的访问者。您可能会为自己节省一些未来的努力,并将 Processor 变成一个抽象类并实现一个具体的 ProcessorImpl 类,从而使将来添加另一个处理器类变得微不足道,或者您可以等到这种情况出现并暂时保持原样.

于 2009-01-09T21:46:06.113 回答
0

太感谢了。这解决了我的问题,我明白双重调度意味着什么!这是后代的完整代码(有人请教我如何正确格式化):

#include <iostream>
using namespace std;

class BaseClass;
class Derived1;
class Derived2;
class Derived3;

class Processor {
public:
       Processor(){}
       virtual void processObj(BaseClass* bc);
       virtual void processObj(Derived1* d1);
       virtual void processObj(Derived2* d2);
};


class BaseClass{
public:
       BaseClass(){}
       virtual void ProcessThis(Processor &p) { p.processObj(this); }
       virtual void myFunction(){cout << "base myFunction called" << endl;}
};

class Derived1: public BaseClass{
public:
       Derived1():BaseClass(){}
       void ProcessThis(Processor &p) { p.processObj(this); }
       void myFunction(){cout << "Derived1 myFunction called" << endl;}
};

class Derived2: public BaseClass{
public:
       Derived2():BaseClass(){}
       void ProcessThis(Processor &p) { p.processObj(this); }
       void myFunction(){cout << "Derived2 myFunction called" << endl;}
};

class Derived3: public BaseClass{
public:
       Derived3():BaseClass(){}
       void ProcessThis(Processor &p) { p.processObj(this); }
       void myFunction(){cout << "Derived3 myFunction called" << endl;}

};

void Processor::processObj(BaseClass* bc){cout << "got a base object" << endl; bc->myFunction();}
void Processor::processObj(Derived1* d1){cout << "got a derived1 object" << endl; d1->myFunction();}
void Processor::processObj(Derived2* d2){cout << "got a derived2 object" << endl; d2->myFunction(); }


int main() {
   BaseClass *bcp=new BaseClass();
   Derived1 *dc1p=new Derived1();   
   Derived2 *dc2p=new Derived2();
   Derived3 *dc3p=new Derived3();

   Processor p;//can also use Processor* p = new Processor()

   //first set results

   bcp->ProcessThis(p);
   dc1p->ProcessThis(p);
   dc2p->ProcessThis(p);
   dc3p->ProcessThis(p);

   BaseClass *bcp1=bcp;
   BaseClass *dc1p1=dc1p;   
   BaseClass *dc2p1=dc2p;
   BaseClass *dc3p1=dc3p;

   //second set results

   bcp1->ProcessThis(p);
   dc1p1->ProcessThis(p);
   dc2p1->ProcessThis(p);
   dc3p1->ProcessThis(p);

   Processor p2;
   bcp1->ProcessThis(p2);
   dc1p1->ProcessThis(p2);
   dc2p1->ProcessThis(p2);
   dc3p1->ProcessThis(p2);

   return 0;
}
于 2009-01-09T22:25:09.327 回答