Results 1 to 3 of 3

Thread: [C++] Double Dispatch

  1. #1
    Sergeant serthy's Avatar
    Join Date
    Nov 2012
    Posts
    450
    Thanks
    96
    Thanked 296 Times in 188 Posts

    [C++] Double Dispatch

    PHP Code:
    #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) const = 0;
            
    virtual void checkCollision( const Sphere) const = 0;
            
    virtual void checkCollision( const Box) const = 0;
    }

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

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

                return 
    v.checkCollision( *this );
            }

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

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

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

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

                return 
    v.checkCollision( *this );
            }

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

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

    int mainint argCount charargs[] )
    {
        
    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.checkCollision);
        
    v.checkCollision);

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

        
    s.checkCollision);
        
    s.checkCollision);
        
    s.checkCollision);

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

        
    b.checkCollision);
        
    b.checkCollision);
        
    b.checkCollision);

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

  2. #2
    ... connecting
    Join Date
    Sep 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by serthy View Post
    PHP Code:
    #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) const = 0;
            
    virtual void checkCollision( const Sphere) const = 0;
            
    virtual void checkCollision( const Box) const = 0;
    }

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

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

                return 
    v.checkCollision( *this );
            }

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

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

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

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

                return 
    v.checkCollision( *this );
            }

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

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

    int mainint argCount charargs[] )
    {
        
    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.checkCollision);
        
    v.checkCollision);

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

        
    s.checkCollision);
        
    s.checkCollision);
        
    s.checkCollision);

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

        
    b.checkCollision);
        
    b.checkCollision);
        
    b.checkCollision);

    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.

  3. #3
    Assadministrator kung foo man's Avatar
    Join Date
    Jun 2012
    Location
    trailerpark
    Posts
    2,013
    Thanks
    2,109
    Thanked 1,086 Times in 753 Posts
    How about using a proper programming language https://en.wikipedia.org/wiki/Multiple_dispatch#Julia
    timescale 0.01

  4. The Following User Says Thank You to kung foo man For This Useful Post:

    kubislav23 (8th September 2018)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •