Inheritance

Member
Posts: 40
Joined: 2008.10
Post: #1
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;
};
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #2
You need to declare them to be virtual in your subclasses, too.
Quote this message in a reply
Apprentice
Posts: 8
Joined: 2008.12
Post: #3
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?
Quote this message in a reply
Member
Posts: 40
Joined: 2008.10
Post: #4
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.
Quote this message in a reply
Apprentice
Posts: 8
Joined: 2008.12
Post: #5
No problem Wink
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Inheritance issues Tekkan 3 2,798 Jun 16, 2007 06:30 PM
Last Post: Tekkan