[SOLVED] Polymorphism Help

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

[SOLVED] Polymorphism Help

Post by GroundUpEngine »

I swear I've done this before and it worked, wtf.. or maybe i'm just tired :(

test.cpp

Code: Select all

#include <iostream>
using namespace std;

class Entity {
  public:
    Entity();
    ~Entity();
};
class Actor : public Entity {
  public:
    Actor() : Entity() {}
    ~Actor() {}
};
class Player : public Actor {
  public:
    Player() : Actor() {}
    ~Player() {}

    int testVar;
    void Shoot() {};
};

int main()
{
    Actor* test = new Player();
    test->testVar = 0;
    test->Shoot();
    
    cin.get();
    delete test;
    return 0;
}
Error: (16) 'class Actor' has no member named 'testVar'
Error: (17) 'class Actor' has no member named 'Shoot'


Purpose: I wanna use it in this function, so I can return a generic Actor or a Player

Code: Select all

ActorManager TestManager;
Actor* TestActor;
TestActor = TestManager.GetPlayer("TestName");
---

Actor* ActorManager::GetPlayer(const string &name)
{
	// Check if the Player is not Already Loaded
	map<string, Actor*>::iterator iter = Actors.find(name);
	if((Actors.size() < MAX_ACTOR_COUNT) && (iter == Actors.end()))
	{
		Actor* act = new Player();
		act->name = name;
		Actors[name] = act;

		cout << name << " is new player\n";
	}
	return Actors[name];
}
Last edited by GroundUpEngine on Wed Apr 14, 2010 10:47 am, edited 1 time in total.
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Polymorphism Help

Post by XianForce »

well, testVar and Shoot() aren't in the Actor class. They are in the player class.

Because you have a pointer to an Actor, you can only use anything an Actor has.

So to solve, either chain up the variable/function or make virtual functions.
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: Polymorphism Help

Post by GroundUpEngine »

XianForce wrote:well, testVar and Shoot() aren't in the Actor class. They are in the player class.

Because you have a pointer to an Actor, you can only use anything an Actor has.

So to solve, either chain up the variable/function or make virtual functions.
Thanks, ye I was looking for an alternative but maybe I'll just use some virtual functions in the base class instead.
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: Polymorphism Help

Post by RyanPridgeon »

You could put it in a Player pointer temporarily to call the function

Code: Select all

int main()
{
    Actor* test = new Player();
    test->testVar = 0;
    Player* temp = test;
    temp->Shoot();
    
    cin.get();
    delete test;
    return 0;
}
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Polymorphism Help

Post by Falco Girgis »

You could also just as easily cast it. XD
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: Polymorphism Help

Post by RyanPridgeon »

GyroVorbis wrote:You could also just as easily cast it. XD
Oh yeah, d'oh
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: Polymorphism Help

Post by GroundUpEngine »

RyanPridgeon wrote:
GyroVorbis wrote:You could also just as easily cast it. XD
Oh yeah, d'oh
My thoughts exactly, Thanks guys! :)
Post Reply