Design Mode: Multiple dispatching and design mode Dispatching

Source: Internet
Author: User

Design Mode: Multiple dispatching and design mode Dispatching

In object-oriented programming, a very common method is to use the pointer of the base class to point to the derived class, and then find the function in the derived class through the virtual function mechanism during the execution period. Suppose this is the case, global functions have two input parameters: A * ap and B * bp. How can we find both the accurate object pointed by ap and the accurate object pointed by bp? The answer is multi-Dispatch. You can find the exact object that ap (bp) points to, and then find the exact object that bp (ap) points to. The sample code is as follows:

#include <iostream>using namespace std;class A {public:virtual void funca() { cout<<"Base A"<<endl; }};class A1 : public A {public:void funca() { cout<<"Derived A1"<<endl; }};class A2 : public A {public:void funca() { cout<<"Derived A2"<<endl; }};class B {public:virtual void funcb(A *p) { cout<<"Base B"<<endl; p->funca();}};class B1 : public B {public: void funcb(A *p) { cout<<"Derived B1"<<endl; p->funca();}};class B2 : public B {public:void funcb(A *p) { cout<<"Derived B2"<<endl; p->funca();}};void func(A *a, B* b) { b->funcb(a); }int main(){B *bp = new B1();A *ap = new A1();func(ap,bp);return 0;}

In the above example, func (A *, B *) is the global function mentioned above, and points ap and bp to the correct derived class object through two-layer virtual function call. Of course, implementing multi-dispatch is not only one method, but also by means of heavy load, as shown below:

class B {public:void funcb(A *p) { cout<<"Base B"<<endl; cout<<"Base A"<<endl;;}void funcb(A1 *p) { cout<<"Base B"<<endl; cout<<"Derived A1"<<endl;}void funcb(A2 *p) { cout<<"Base B"<<endl; cout<<"Derived A2"<<endl;}};
In this case, B needs to know which derived classes A has, and then define an overloaded version of A function for each of the derived classes of, in A, you can pass the this pointer to the call to B, as shown below:

class A {public:virtual void funca(B *p) { p->funcb(this);}};class A1 : public A {public:void funca(B *p) { p->funcb(this); }};class A2 : public A {public:void funca(B *p) { p->funcb(this);}};
Global functions are defined as follows:

void func(A *ap, B* bp) { ap->funca(bp); }
In this way, the object of the derived class of A is first found through the virtual function call, and then the this pointer is passed to the object of the derived class of A to the call of B, at this time, the passed this pointer is already a pointer to the derived class, so it can help B select the correct function overload version.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.