CBaseEntity *currentent;
CBaseDynamic *dyn;
currentent = primaryent;
while(1)
{
if(currentent->Collide && currentent->Active)
{
dyn = (CBaseDynamic*)currentent;
dyn->Iterate();
}
else
currentent->Iterate();
if(currentent == finalent)
break;
else
{
currentent = currentent->next;
}
}
🔒 Locked
Derived Classes Problem
Advertisement
Original Post
I have a base entity class that everything''s derived from. Now each class has an Iterate function, that''s called everyframe, for things like physics and anything else that needs to be done.
My problem is that with the code I currently have it only calls the base entity function and not the overridden Iterate function.
Anyway here''s a simplified version of my code:
You''ll see that it goes through a linked list. It currently has a hack that allows physics to be carried out, but this isn''t satisfactory, I could extend the hack to all entities, but that wopuld get ridiculously long.
So how do I make it so it always calls the ''highest'' function, the most complicated one?
Thanks in advance,
Higherspeed
Your base class Iterate() function needs to be virtual. Your derived class methods need to have the same signiture (const-ness, call parameters, etc).
ie
class BaseClass
{
virtual void Iterate() {}
};
class Derived : public BaseClass
{
virtual void Iterate() {}
}
Calling a BaseClass*''s Iterate() should call the most derived class.
ie
class BaseClass
{
virtual void Iterate() {}
};
class Derived : public BaseClass
{
virtual void Iterate() {}
}
Calling a BaseClass*''s Iterate() should call the most derived class.
Now this is where all my knowledge breaks down. I can make fabrics and rippling water with ease(well maybe not), but I suppose I should have learnt the basics first.
My answer to your question:
It is now.
One other question. Is it sufficient to make the base entity function virtual? Or do later functions that are overridden need this as well?
My answer to your question:
It is now.
One other question. Is it sufficient to make the base entity function virtual? Or do later functions that are overridden need this as well?
quote:
Original post by higherspeed
One other question. Is it sufficient to make the base entity function virtual? Or do later functions that are overridden need this as well?
The virtual modifier is automatically inherited, so you don''t need to explicitly specify CDerived::Iterate as being virtual, but it does make things more clear...
You should make it a pure virtual function, so your program will crash if it calls the (meaningless) base class method.
//declare it like so in the base class:
class base{
public:
virtual void function() = 0;
};
//and in the inherited:
class inh : public base{
public:
void function(){ cout << "inherited" << endl; }
}
so that way base::function() crashes your program, and inh::function() will run fine...just for peace of mind...
Well actaully each iterate function calls the iterate function which it has been derived from, so a pure function may not be suitable.
What is the advantage of using a pure function? Is it quicker? My friend keeps asking if it wears white underwear, but you should probably ignore that.
Thanks,
Higherspeed
What is the advantage of using a pure function? Is it quicker? My friend keeps asking if it wears white underwear, but you should probably ignore that.
Thanks,
Higherspeed
Pure virtual functions are used to specify interfaces; that is, when you have a base class that is so abstract that there is no meaningful implementation. For instance, in the classic OO example of a Shape hierarchy (with Squares, Circles, and so forth), there really isn''t a general way to draw a Shape; therefore, a draw() function present in the base class should be made pure virtual. This ensures that no one can instantiate this class and call its draw() function, which is a good thing since it could not possibly do anything useful.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement