• Advertisement
Sign in to follow this  

Question regarding inheritance

This topic is 2384 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Given the following:

#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);
};


Shouldn't the following work?

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);
}


I'm getting the following error:

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

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
Advertisement
[s]Is MobType a subclass of BaseEntity?[/s]

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

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.


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

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?

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 pointer-to-derived, 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 pointer-to-derived 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
Alternately you can make your factory class a template type (possibly using a base interface and covariant return types).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement