Jump to content
  • Advertisement
Sign in to follow this  
BaneTrapper

Polymorphisam issue, what is the way to resolve?

This topic is 2098 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

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.

Edited by BaneTrapper

Share this post


Link to post
Share on other sites
Advertisement
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
{
    // ...
};

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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*.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!