Inheritance
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:
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;
};
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:
Then running the following instructions properly called the derived func()...
Do you have more code to share with us?
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?
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.
Base * instance = new Derived();
I was using:
Base instance;
Derived d;
instance = d;
Sorry.
No problem
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Inheritance issues | Tekkan | 3 | 2,578 |
Jun 16, 2007 06:30 PM Last Post: Tekkan |
|

