![]() |
|
Pointers and Polymorphism; |
Aaron Santiago
Member #12,074
June 2010
![]() |
game::baseEntity * playerPointer = new game::playerEntity(); game::playerEntity * player = playerPointer;
where playerEntity is a subclass of baseEntity. Thanks in advance. |
J-Gamer
Member #12,491
January 2011
![]() |
Reverse it? 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 |
blargmob
Member #8,356
February 2007
![]() |
You have defined playerPointer as type baseEntity. You can't go backwards like that without an explicit cast. --- |
Aaron Santiago
Member #12,074
June 2010
![]() |
Well, that was easy. |
blargmob
Member #8,356
February 2007
![]() |
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. --- |
J-Gamer
Member #12,491
January 2011
![]() |
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 |
blargmob
Member #8,356
February 2007
![]() |
Read his OP. He wants have a handle to playerPointer so he can interact with it directly for some reason. --- |
Aaron Santiago
Member #12,074
June 2010
![]() |
Directly as in not through virtual member functions. |
blargmob
Member #8,356
February 2007
![]() |
Exactly. So I don't see why you would do it that way.
--- |
Aaron Santiago
Member #12,074
June 2010
![]() |
You've lost me. |
blargmob
Member #8,356
February 2007
![]() |
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
--- |
Aaron Santiago
Member #12,074
June 2010
![]() |
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
![]() |
Aaron Santiago said: 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 --- |
Aaron Santiago
Member #12,074
June 2010
![]() |
Jesse Lenney said: It's worded backwards in the OP
Aaron Santiago said: 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. |
bamccaig
Member #7,536
July 2006
![]() |
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. -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
Jonatan Hedborg
Member #4,886
July 2004
![]() |
You forgot a "*" I think.
|
bamccaig
Member #7,536
July 2006
![]() |
I don't know what you're talking about.[1] References
-- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
|