PDA

View Full Version : [C++] Double Dispatch



serthy
29th July 2013, 10:40
#include <iostream>

//forward declerations of derived classes
class Sphere;
class Box;

class Volume //my base class
{
public:

//virtual, non-base-class methods
virtual void checkCollision( const Volume& v ) const = 0;
virtual void checkCollision( const Sphere& s ) const = 0;
virtual void checkCollision( const Box& b ) const = 0;
}

class Sphere : public Volume //derived 1
{
public:

void checkCollision( const Volume& v ) const
{
std::cout << "redo: sphere vs volume" << std::endl;

return v.checkCollision( *this );
}

void checkCollision( const Sphere& s ) const
{
std::cout << "sphere vs sphere" << std::endl;
}

void checkCollision( const Box& b ) const
{
std::cout << "sphere vs box" << std::endl;
}
}

class Box : public Volume //derived 2
{
public:

void checkCollision( const Volume& v ) const
{
std::cout << "redo: box vs volume" << std::endl;

return v.checkCollision( *this );
}

void checkCollision( const Sphere& s ) const
{
std::cout << "box vs sphere" << std::endl;
}

void checkCollision( const Box& b ) const
{
std::cout << "box vs box" << std::endl;
}
}

int main( int argCount , char* args[] )
{
Volume v;
Sphere s;
Box b;

//doesnt work, because of virtual = 0; methods in base class, comment them out then
std::cout << "VOLUME:" << std::endl;

v.checkCollision( v );
v.checkCollision( s );
v.checkCollision( b );

std::cout << "SPHERE:" << std::endl;

s.checkCollision( v );
s.checkCollision( s );
s.checkCollision( b );

std::cout << "BOX:" << std::endl;

b.checkCollision( v );
b.checkCollision( s );
b.checkCollision( b );
}

My understanding of double dispatch, is there an easier and maybe more dynamic method (for adding new derived classes?) without any casting methods?

RoyWillis
5th September 2018, 19:20
#include <iostream>

//forward declerations of derived classes
class Sphere;
class Box;

class Volume //my base class
{
public:

//virtual, non-base-class methods
virtual void checkCollision( const Volume& v ) const = 0;
virtual void checkCollision( const Sphere& s ) const = 0;
virtual void checkCollision( const Box& b ) const = 0;
}

class Sphere : public Volume //derived 1
{
public:

void checkCollision( const Volume& v ) const
{
std::cout << "redo: sphere vs volume" << std::endl;

return v.checkCollision( *this );
}

void checkCollision( const Sphere& s ) const
{
std::cout << "sphere vs sphere" << std::endl;
}

void checkCollision( const Box& b ) const
{
std::cout << "sphere vs box" << std::endl;
}
}

class Box : public Volume //derived 2
{
public:

void checkCollision( const Volume& v ) const
{
std::cout << "redo: box vs volume" << std::endl;

return v.checkCollision( *this );
}

void checkCollision( const Sphere& s ) const
{
std::cout << "box vs sphere" << std::endl;
}

void checkCollision( const Box& b ) const
{
std::cout << "box vs box" << std::endl;
}
}

int main( int argCount , char* args[] )
{
Volume v;
Sphere s;
Box b;

//doesnt work, because of virtual = 0; methods in base class, comment them out then
std::cout << "VOLUME:" << std::endl;

v.checkCollision( v );
v.checkCollision( s );
v.checkCollision( b );

std::cout << "SPHERE:" << std::endl;

s.checkCollision( v );
s.checkCollision( s );
s.checkCollision( b );

std::cout << "BOX:" << std::endl;

b.checkCollision( v );
b.checkCollision( s );
b.checkCollision( b );
}

My understanding of double dispatch, is there an easier and maybe more dynamic method (for adding new derived classes?) without any casting methods?

You can get the double dispatch code link here : https://gist.github.com/s3rvac/ddb2a0ffc6bcc1d3b2f6
Without type cast.

kung foo man
6th September 2018, 10:49
How about using a proper programming language https://en.wikipedia.org/wiki/Multiple_dispatch#Julia