Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Pointers and Polymorphism;

Credits go to blargmob and J-Gamer for helping out!
This thread is locked; no one can reply to it. rss feed Print
Pointers and Polymorphism;
Aaron Santiago
Member #12,074
June 2010
avatar

game::baseEntity * playerPointer = new game::playerEntity();
game::playerEntity * player = playerPointer;

where playerEntity is a subclass of baseEntity.
What I want is two pointers to the same playerEntity, one as a baseEntity pointer and the other as a playerEntity pointer. The former will be used to put it in with the rest of the objects in the collision detection code, and the latter will be used to interact with the playerEntity directly.
The above code does not compile, saying "error: invalid conversion from 'game::baseEntity*' to 'game::playerEntity*'"
How would I do what I want?

Thanks in advance.

J-Gamer
Member #12,491
January 2011
avatar

Reverse it?
First declare the player pointer, then the base pointer.

game::playerEntity * player = new game::playerEntity();
game::baseEntity * playerPointer = player;

This is because the compiler doesn't know if the baseEntity pointer you declared in the first line is a playerEntity or a baseEntity.

" There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo
"If your body was a business, thought would be like micro-management and emotions would be like macro-management. If you primarily live your life with emotions, then you are prone to error on the details. If you over-think things all the time you tend to lose scope of priorities." - Mark Oates

blargmob
Member #8,356
February 2007
avatar

You have defined playerPointer as type baseEntity.

You can't go backwards like that without an explicit cast.

---
"No amount of prayer would have produced the computers you use to spread your nonsense." Arthur Kalliokoski

Aaron Santiago
Member #12,074
June 2010
avatar

Well, that was easy. :D
Thanks!

blargmob
Member #8,356
February 2007
avatar

J-Gamer's suggestion isn't fitted to what you want to do. You'll have to cast every time you want to work with playerPointer.

Just keep your code as it was and take care of that cast right there.

---
"No amount of prayer would have produced the computers you use to spread your nonsense." Arthur Kalliokoski

J-Gamer
Member #12,491
January 2011
avatar

Why can't he put PlayerPointer in the container with other baseEntity's? I don't understand why you would need to cast it again.

" There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo
"If your body was a business, thought would be like micro-management and emotions would be like macro-management. If you primarily live your life with emotions, then you are prone to error on the details. If you over-think things all the time you tend to lose scope of priorities." - Mark Oates

blargmob
Member #8,356
February 2007
avatar

Read his OP. He wants have a handle to playerPointer so he can interact with it directly for some reason.

---
"No amount of prayer would have produced the computers you use to spread your nonsense." Arthur Kalliokoski

Aaron Santiago
Member #12,074
June 2010
avatar

Directly as in not through virtual member functions.

blargmob
Member #8,356
February 2007
avatar

Exactly. So I don't see why you would do it that way.

???

---
"No amount of prayer would have produced the computers you use to spread your nonsense." Arthur Kalliokoski

Aaron Santiago
Member #12,074
June 2010
avatar

You've lost me.
What J-Gamer suggested worked perfectly, the group call to all of my entities' draw functions worked, and the calls to the playerEntity-unique functions all worked.
What were you saying about casting? ???

blargmob
Member #8,356
February 2007
avatar

If you need a handle to a child class so that you can call explicitly defined methods in that class, then the type for the handle should be the type of the child, not the parent.

public Car : Vehicle { ... }

Car myCar = new Car(); //correct
Vehicle myCar = new Car(); //incorrect

---
"No amount of prayer would have produced the computers you use to spread your nonsense." Arthur Kalliokoski

Aaron Santiago
Member #12,074
June 2010
avatar

Right. That's why there are two pointers. The first is the one I use to handle explicitly defined methods in playerEntity, and the second is the one I put into my baseEntity vector.

blargmob
Member #8,356
February 2007
avatar

The first is the one I use to handle explicitly defined methods in playerEntity, and the second is the one I put into my baseEntity vector.

It's worded backwards in the OP :-[

---
"No amount of prayer would have produced the computers you use to spread your nonsense." Arthur Kalliokoski

Aaron Santiago
Member #12,074
June 2010
avatar

It's worded backwards in the OP

What I want is two pointers to the same playerEntity, one as a baseEntity pointer and the other as a playerEntity pointer. The former will be used to put it in with the rest of the objects in the collision detection code, and the latter will be used to interact with the playerEntity directly.

It's confusing, I'll give you that, but I worded it properly. :D

bamccaig
Member #7,536
July 2006
avatar

You technically don't need to define a game::baseEntity * at all if you're just storing it in a container next. You could just store it.

std::vector<game::baseEntity *> objects;

game::playerEntity * player = new game::playerEntity();

objects.push_back(player);

You may want to still create the base pointer first if you do anything else with it as a base before storing it.

Jonatan Hedborg
Member #4,886
July 2004
avatar

You forgot a "*" I think.

bamccaig
Member #7,536
July 2006
avatar

Go to: