Sign in to follow this  
Quasimojo

Question regarding inheritance

Recommended Posts

Given the following:

[code]#include <vector>
#include "entities.h"

typedef unsigned long GLOBALID;

using namespace std;

class BaseEntity {
char[10] Name;
};

class Mob : public BaseEntity {
Mob() { Name = "Orc"; }
};

class Player : public BaseEntity {
Player() { Name = "Frodo"; }
};

class Factory_Entity {
public:
vector<BaseEntity*> EntityList;
protected:
BaseEntity* TrackEntity(BaseEntity*);
};

class Factory_Mob : public Factory_Entity {
public:
Mob* CreateMob();
};

class Factory_Player : public Factory_Entity {
public:
Player* CreatePlayer(GLOBALID);
protected:
Player* LoadPlayer(GLOBALID);
};[/code]

Shouldn't the following work?

[code]BaseEntity* Factory_Entity::TrackEntity(BaseEntity* entity)
{
EntityList.push_back(entity);
return EntityList.back(); // Error occurring on this line
}
Mob* Factory_Mob::CreateMob(MobType* mobType)
{
Mob* mob = new Mob();

// Add mob to global mob list
return Factory_Entity::TrackEntity(mob);
}[/code]

I'm getting the following error:

[code]entityfactory.cpp:14: error: invalid conversion from ‘BaseEntity*’ to ‘Mob*’ [-fpermissive][/code]

My understanding was that derived classes could be used wherever the base class is expected, as a derived class *is* a version of the base class. Where am I off base?

Share this post


Link to post
Share on other sites
The problem is that TrackEntity returns a BaseEntity*. In CreateMob() you try to return a BaseEntity* when the return type is Mob*. BaseEntity* will not implicitly convert into a Mob*. A Mob* on the other hand will happily convert to a BaseEntity* because Mob is a subclass of BaseEntity.

Share this post


Link to post
Share on other sites
[quote name='Wooh' timestamp='1313454168' post='4849637']
The problem is that TrackEntity returns a BaseEntity*. In CreateMob() you try to return a BaseEntity* when the return type is Mob*. BaseEntity* will not implicitly convert into a Mob*. A Mob* on the other hand will happily convert to a BaseEntity* because Mob is a subclass of BaseEntity.
[/quote]

Ok. I understand. I was trying to avoid duplicating the TrackEntity code specifically for Mobs and Players, just for a different return type. Is there a way to do that in this situation?

Share this post


Link to post
Share on other sites
[quote name='Quasimojo' timestamp='1313455840' post='4849647']
Ok. I understand. I was trying to avoid duplicating the TrackEntity code specifically for Mobs and Players, just for a different return type. Is there a way to do that in this situation?
[/quote]
In this case you should be able to [font="Courier New"]static_cast<>[/font] safely. The code you pasted guarantees the dynamic type of the returned value to be a [i]pointer-to-derived[/i], which means [font="Courier New"]static_cast<>[/font] is just oil telling the compiler you know what you're doing and to relax the rules for just this once. If you could not guarantee the returned value is a [i]pointer-to-derived [/i]of the right type, you would be forced to use [font="Courier New"]dynamic_cast<>[/font] and check the result.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this