C++ inheritance in an array
If I have a class, say, creature, and I have a bunch of inherited classes with some reimplemented methods, how will I make an array to hold a bunch of different subclasses and still call each one''s overwritten methods?
Just use an array of pointers to the base class. It''s safe to assign a pointer to a subclass to a pointer to its base class, and when you call a virtual function through a base-class pointer, you''ll automatically call the appropriate version of the function.
You need to use virtual classes/function....
eg you have a creature class, define the functions you want to override as virtual....
class Creature
{
private:
.
.
.
public:
virtual void Attack( void );
virtual void Move( void );
.
.
.
}
and then you have your derived classes
class FlyingCreature : Creature
{
private:
public:
void Attack( void );
void Move( void );
.
.
}
class GroundCreature : Creature
{
private:
public:
void Attack( void );
void Move( void );
.
.
}
and in their .cpp files you define the different creatures funtcions....eg
void FlyingCreature::Attack( void )
{...
}
void FlyingCreature::Move( void )
{...
}
void GroundCreature::Attack( void )
{...
}
void GroundCreature::Move( void )
{...
}
Okay so if you now want to create an array of mixed types of creatures, and move them, you can do:
Creatures *pCreature[ 10 ];
void CreateCreatures( void )
{
int x;
for ( x=0 ; x<5 ; x++ )
{
pCreature[ x ] = new FlyingCreature;
}
for ( x=5 ; x<10 ; x++ )
{
pCreature[ x ] = new GroundCreature;
}
}
and then to move them
void MoveCreatures( void )
{
int x;
for( x=0 ; x<10 ; x++ )
{
pCreature[ x ]->Move();
}
}
-----------------------------------------------
Basically once you declare any of a classes functions as virtual, the program will check which type of object you''re actually trying to access rather than assuming it''s of the same type as the pointer as you''re passing....
-----------------------------------------------
ps if this doesn''t make sense it''s probably something to do with the fact that I''m on holiday in Amsterdam 8-)
Game production:
Good, quick, cheap: Choose two.
eg you have a creature class, define the functions you want to override as virtual....
class Creature
{
private:
.
.
.
public:
virtual void Attack( void );
virtual void Move( void );
.
.
.
}
and then you have your derived classes
class FlyingCreature : Creature
{
private:
public:
void Attack( void );
void Move( void );
.
.
}
class GroundCreature : Creature
{
private:
public:
void Attack( void );
void Move( void );
.
.
}
and in their .cpp files you define the different creatures funtcions....eg
void FlyingCreature::Attack( void )
{...
}
void FlyingCreature::Move( void )
{...
}
void GroundCreature::Attack( void )
{...
}
void GroundCreature::Move( void )
{...
}
Okay so if you now want to create an array of mixed types of creatures, and move them, you can do:
Creatures *pCreature[ 10 ];
void CreateCreatures( void )
{
int x;
for ( x=0 ; x<5 ; x++ )
{
pCreature[ x ] = new FlyingCreature;
}
for ( x=5 ; x<10 ; x++ )
{
pCreature[ x ] = new GroundCreature;
}
}
and then to move them
void MoveCreatures( void )
{
int x;
for( x=0 ; x<10 ; x++ )
{
pCreature[ x ]->Move();
}
}
-----------------------------------------------
Basically once you declare any of a classes functions as virtual, the program will check which type of object you''re actually trying to access rather than assuming it''s of the same type as the pointer as you''re passing....
-----------------------------------------------
ps if this doesn''t make sense it''s probably something to do with the fact that I''m on holiday in Amsterdam 8-)
Game production:
Good, quick, cheap: Choose two.
Game production:Good, quick, cheap: Choose two.
Just one more thing, guys. By the way, thanks for the input. I never understood virtual functions before.
My next problem: Can you define an array of data types? I mean take the following example:
class creature
{
...
};
class flyingcreature : creature
{
...
}
class groundcreature : creature
{
...
}
int main()
{
sometype Types[2];
Types[1] = flyingcreature
Types[2] = groundcreature
creature* pCreature;
int kind;
cin<pCreature = new Types[kind];
...
}
You see what I''m looking for? This way, I could use an index for what type I would be creating, saving myself a bunch of switch''s.
My next problem: Can you define an array of data types? I mean take the following example:
class creature
{
...
};
class flyingcreature : creature
{
...
}
class groundcreature : creature
{
...
}
int main()
{
sometype Types[2];
Types[1] = flyingcreature
Types[2] = groundcreature
creature* pCreature;
int kind;
cin<pCreature = new Types[kind];
...
}
You see what I''m looking for? This way, I could use an index for what type I would be creating, saving myself a bunch of switch''s.
No, new can only operate/understand one type of class at a time (I believe).
It wouldn''t be that much code you''d need to write if you just create a wrapper function....
Creature *CreateCreature( someType Type )
{
switch(...)
{
case("FlyingCreature"):
return new FlyingCreature;
break;
case("GroundCreature"):
return new FlyingCreature;
break;
}
}
, unless you have loads of different creatures....in which case you''re going to have to write loads of code to do stuff with them anyway.
dan
Game production:
Good, quick, cheap: Choose two.
Game production:Good, quick, cheap: Choose two.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement