iDevGames Forums
Inheritance - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: Inheritance (/thread-1727.html)



Inheritance - merrill541 - Feb 22, 2009 02:08 PM

I have been trying to write a Constraint class, and then making other classes that inherit from it, but the virtual functions I defined in the original constraint class keep on being called, and not the ones in my derived class.

Here is the code:
Code:
class GlobalConstraint{
    public:
        virtual void update(float timestep, Particle *a)
        {
        }
        virtual void cancel()
        {
        }
        virtual bool satisfy(Vector p)
        {
            return true;
        }
};

class BoxConstraint: public GlobalConstraint{
    public:
        BoxConstraint(Vector theMin, Vector theMax, float theBounciness)
        {
            min = theMin;
            max = theMax;
            bounciness = theBounciness;
            doing = true;
        }
        BoxConstraint(Vector theMin, Vector theMax)
        {
            min = theMin;
            max = theMax;
            bounciness = DEFAULT_BOUNCINESS;
            doing = true;
        }
        bool satisfy(Vector p)
        {
            if(((p.x>min.x&&p.x<max.x)&&(p.y>min.y&&p.y<max.y))&&(p.z>min.z&&p.z<max.z))
                return false;
            else
                return true;
        }
        void update(float timestep, Particle *a)
        {
            if(doing)
            {
                if(!satisfy(a->pos))
                {
                    a->pos = a->oldPos;
                }
            }
        }
        void cancel()
        {
            doing = false;
        }
    private:
        Vector min;
        Vector max;
        bool doing;
        float bounciness;
};



Inheritance - DoG - Feb 23, 2009 01:54 AM

You need to declare them to be virtual in your subclasses, too.


Inheritance - ozirus - Feb 23, 2009 04:18 AM

As far as I know, it should not be necessary to redeclare the methods as virtual in the derived class. Once a method has been declared virtual, it will remain virtual in all derived classes.

Nevertheless, I wrote the small code sample below in XCode to check this behavior:

Code:
class Base
{
public:
    virtual void func()
    {
        printf( "Base\n" );
    }
};

class Derived : public Base
{
public:
    Derived()
    {
    }
    
    virtual ~Derived()
    {
    }
    
    void func()
    {
        printf( "Derived\n" );
    }
};

Then running the following instructions properly called the derived func()...

Code:
Base * instance = new Derived();
instance->func();

Do you have more code to share with us?


Inheritance - merrill541 - Feb 23, 2009 07:29 AM

I actually figured out what the problem was, I wasn't using a pointer for my classes. As in:

Base * instance = new Derived();

I was using:

Base instance;
Derived d;
instance = d;

Sorry.


Inheritance - ozirus - Feb 23, 2009 07:37 AM

No problem Wink