Advertisement

Polymorphisam issue, what is the way to resolve?

Started by January 19, 2014 10:02 AM
3 comments, last by Melkon 11 years ago

Hello.

I have a following situation


class Unit
{
public:
    Unit(float startPosX, float startPosY, float MovementRate, int unitSizeX, int unitSizeY, int sprTexID);
};

class UnitPlayerControled :public Unit, public UnitProperties, public EntityBase
{
public:
    UnitPlayerControled(float startPosX, float startPosY, float MovementRate, int unitSizeX, int unitSizeY, int sprTexID);
};

class UnitList
{
public:
    std::vector<std::unique_ptr<Unit>> unitList;
};

//This is the code i need to call
void EntityManager::AddUnitEntity(EntityBase * unit)
{//...
}

int main()
{
UnitList unitList;
EntityManager objEntity;
//...
    unitList.unitList.push_back(std::unique_ptr<Unit>(new UnitPlayerControled(0,0,0,24,24,1)));
    unitList.unitList.push_back(std::unique_ptr<Unit>(new UnitPlayerControled(100,100,0,24,24,3)));
    unitList.unitList.push_back(std::unique_ptr<Unit>(new UnitPlayerControled(199,199,0,24,24,500)));
    objEntity.AddUnitEntity(*(unitList.unitList[0]));//Issue is here
    objEntity.AddUnitEntity(*(unitList.unitList[1]));//cannot convert parameter 1 from 'Unit' to 'EntityBase *'
    objEntity.AddUnitEntity(*(unitList.unitList[2]));
}

I get error "cannot convert parameter 1 from 'Unit' to 'EntityBase *'" and i do not know to to resolve

I do not know how to pass to the "EntityBase * unit" from the Entity class from UnitPlayerControled.

What is the way i would i execute something like this.

Assuming what you want is for Unit to be a type of EntityBase, you need to specify that Unit is derived from EntityBase, as such:

class Unit : public EntityBase
{
    // ...
};

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Advertisement

unitList contains (unique pointers to) Unit instances. A Unit stands in absolutely no relation to EntityBase. I'm not too firm the hell that C++ becomes once you add multiple inheritance, but you probably manually have to cast the pointer back into UnitPlayerControled (and then the compiler can automatically convert EntityBase) to get a safe conversion.

That said, everything in me screams "Noooo!" when it sees that design. If you need to manually cast something it usually already marks a potentially broken design. And then you add multiple inheritance just for kicks. Maybe you want to investigate other patterns, like composition? Combining composition with what ApochPiQ sounds much saner right out of the box.

It truely is a design flaw i overlooked this issue... It seams the way i want to approach is not with polymorphisam.

In both ways there is so much work to be done with what i have alredy on the table and that is only beggining.

Thank you for quick respond!

If you really need an Add, that wait for an EntityBase* why don't you have 2 function for AddEntity?

One get an EntityBase*, and one get a Unit*.

This topic is closed to new replies.

Advertisement