Double dispatch

The principle of secondary dispatch technology

  ① Dispatch: Selecting a method according to the type of object is dispatch. Static dispatch occurs at compile time. The dispatch specifies a method based on static type information, which is a static binding of a method (such as method overloading). Dynamic dispatch occurs at runtime and calls a method based on the actual type of the object to which the method belongs. Dynamic type binding is only reflected in the caller of the method, and the parameter types of the method are determined by the compiler at compile time.

  ②Single dispatch : The method to be called is specified only according to the actual type of the object to which the method belongs and the static type of the parameter .

  ③Multiple dispatch : The method to be called is specified according to the actual type of the object to which the method belongs and the actual type of the parameter .

[Experimental analysis] Simulation of single dispatch support and double dispatch strategy in C++

//Problem: C++ only supports single dispatch, that is, dispatch only based on the function name and the static type of the parameter (not the actual type). How do I simulate a double dispatch?
//Solution ideas: see the analysis below:

#include <iostream>
using namespace std;


class CProblem;//Forward declaration of general problems
class CSpecialProblem;//Forward declaration of special problems

// first level support
class CSupporter{
public:
	virtual void Solve(CProblem* p){ cout << "CSupporter::Solve(CProblem)" << endl;}
	virtual void Solve(CSpecialProblem* sp){ cout << "CSupporter::Solve(CSpecialProble)" << endl;}
};

//Senior support
class CSeniorSupporter : public CSupporter{
public:
	virtual void Solve(CProblem* p){ cout << "CSeniorSupporter::Solve(CProblem)" << endl;}
	virtual void Solve(CSpecialProblem* sp){ cout << "CSeniorSupporter::Solve(CSpecialProblem)" << endl;}
};

// general questions
class CProblem{
public:
	//Pass the Problem itself to s->solve(), here is the key
	virtual void Accept(CSupporter* s){s->Solve(this);}
};

//special problem
class CSpecialProblem : public CProblem{
public:
	//Pass SpecialProblem itself to s->solve(), here is the key
	virtual void Accept(CSupporter* s){s->Solve(this);}
};


void DobuleDispatch()
{
	CProblem* pProblem = new CProblem();//General problems
	CProblem* pSpecialProblem = new CSpecialProblem();//Special problem [note, it is the parent class pointer]
	
	CSupporter* pSupporter = new CSeniorSupporter();//Senior solution expert


	cout << "Demonstrate the single dispatch phenomenon of C++ language" << endl;
	pSupporter->Solve(pProblem);//The parameter type is static binding
	pSupporter->Solve(pSpecialProblem);//The parameter type is static binding

	cout << endl << "Demonstrate the simulated double dispatch strategy" << endl;
	pProblem->Accept(pSupporter);
	pSpecialProblem->Accept(pSupporter);
	//1st dispatch: find the CSpecialProblem::Accept(CSupporter*) function according to the actual type of pSpecialProblem at runtime
    //Second dispatch: When s->Solve(this) is executed inside the above function, the CSeniorSupporter::Solve(CSpecialProblem*) function will be found according to the actual type of s
}
 /*
 Output result:
Demonstrate the single dispatch phenomenon of the C++ language
CSeniorSupporter::Solve(CProblem)
CSeniorSupporter::Solve(CProblem)

Demonstrate the simulated double dispatch strategy
CSeniorSupporter::Solve(CProblem)
CSeniorSupporter::Solve(CSpecialProblem)
 */

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324805129&siteId=291194637