Double dispatch

Principle of secondary dispatch technology

① dispatch: select methods according to the type of objects, that is, dispatch. Static dispatch occurs at compile time. Dispatch specifies methods according to static type information. It is a static binding of methods (such as method overloading). Dynamic dispatch occurs at runtime and calls methods based on the actual type of the object to which they belong. Dynamic type binding is only reflected in the caller of the method, while the parameter type of the method is determined by the compiler at compile time.

(2) single dispatch: only the actual type of the object to which the method belongs and the static type of the parameter are used to specify the called method.

③ multi 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 for C + +

//Problem: C + + only supports single dispatch, that is, dispatch only according to the static type (not the actual type) of function name and parameter. How to simulate double dispatch?
//Solution: see the following analysis:
 
#include <iostream>
using namespace std;
 
 
class CProblem;//Forward statement of general issues
class CSpecialProblem;//Forward statement 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 problems
class CProblem{
public:
	//Pass the Problem itself to s - > solve(), which is the key
	virtual void Accept(CSupporter* s){s->Solve(this);}
};
 
//Special problems
class CSpecialProblem : public CProblem{
public:
	//Pass the SpecialProblem itself to s - > solve(), which 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's a parent pointer]
	
	CSupporter* pSupporter = new CSeniorSupporter();//Senior Solution Specialist
 
 
	cout << "Demonstration C++Single assignment of language" << endl;
	pSupporter->Solve(pProblem);//Parameter type is static binding
	pSupporter->Solve(pSpecialProblem);//Parameter type is static binding
 
	cout << endl << "Demonstrate the dual dispatch strategy of simulation" << endl;
	pProblem->Accept(pSupporter);
	pSpecialProblem->Accept(pSupporter);
	//Dispatch 1: find the cspecialproblem:: accept (cssupporter *) function at runtime according to the actual type of pSpecialProblem
    //Dispatch 2: when executing s - > solve (this) inside the above function, cseniorsupplier:: solve (cspecialproblem *) function will be found according to the actual type of S
}

 /*
Output results:
Demonstration of single dispatch in C + + language
CSeniorSupporter::Solve(CProblem)
CSeniorSupporter::Solve(CProblem)
Demonstrate the dual dispatch strategy of simulation
CSeniorSupporter::Solve(CProblem)
CSeniorSupporter::Solve(CSpecialProblem)
 */

Posted on Thu, 09 Jan 2020 12:32:42 -0500 by socadmin