Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Accessing child members from parent class

Author
Message
Malboro Jones
14
Years of Service
User Offline
Joined: 10th Dec 2009
Location: Wales - UK
Posted: 4th Dec 2010 04:26 Edited at: 4th Dec 2010 05:56
IGNORE ALL THIS, Go to my second comment

Hiiiiiii, I'm having an issue accessing members of a class from its parent, I know it can be done using virtual members something like this:



but I cant get it to work.
I'm on half making a final fantasy type RPG and I'm trying to get the fight scene done.

I've got an EnemyClass that has all the variables such as level, hp, mp and then child classes for each type of enemy which will have functions such as load_object, kill etc.

I will have an array EnemyClass enemies[3] and I create the 3 child classes and assign them to 0, 1 and 2 then from a "start_fight" function I want to be able to do something like enemies[1]->load_object() but it always calls the load_object from the EnemyClass and not the child.

Quite complicated I know any ideas how to get it to work or an example?

Thanks, Mal

Edit:

I just did this quick test:


and it works fine, but in my other version it doesn't I would post the code but there's hundreds of lines so I'll take another look, if I get desperate I'll put the code up.

Edit 2:

I figured out why I was having a problem, I was creating the "Guard" class in a function and assigning it to "Fight::Enemies[]" from there so once the function was done the pointer to the classes I was giving it don't work, declaring them outside worked but that would mean they always exist even if I don't want them oh well I'll figure something out

Don't make war, make tea.
WLGfx
16
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 4th Dec 2010 05:04 Edited at: 4th Dec 2010 05:05
You need to assign them to public:
Also add ';' at the end of the class descriptor...

Warning! May contain Nuts!
Malboro Jones
14
Years of Service
User Offline
Joined: 10th Dec 2009
Location: Wales - UK
Posted: 4th Dec 2010 05:29 Edited at: 4th Dec 2010 05:31
not sure what you're on about but thanks.
I sort of managed it if you read my second edit, I'll find a way around what I want hopefully.

edit:

Oh I know what you mean, you're talking about the first code I put up,
that was meant to be just an example I know that snippet had problems but thanks.

Don't make war, make tea.
Malboro Jones
14
Years of Service
User Offline
Joined: 10th Dec 2009
Location: Wales - UK
Posted: 4th Dec 2010 05:38 Edited at: 4th Dec 2010 06:40
Right, this is my problem now, I've recreated the problem to shorten it because my version is much more complex:



That always shows "A::doPrint", instead of "Barry" and "Cath"

Whyyyyyyyyyyyyyy?

Edit:

YAAAAAAAY, memcpy to the rescue :



that way seems to have worked fine, I use memcpy then everything seems to work as I want it

Don't make war, make tea.
Bran flakes91093
16
Years of Service
User Offline
Joined: 13th Sep 2008
Location: Crazy Land
Posted: 4th Dec 2010 18:59
Quote: "That always shows "A::doPrint", instead of "Barry" and "Cath"

Whyyyyyyyyyyyyyy?"


Because polymorphism only works with pointers. You are creating objects, which inherit only the base class that they have in common.

http://www.cplusplus.com/doc/tutorial/polymorphism/

“C++ : Where friends have access to your private members.”
-Gavin Russell Baker
Malboro Jones
14
Years of Service
User Offline
Joined: 10th Dec 2009
Location: Wales - UK
Posted: 8th Dec 2010 16:55
Ahhhh I havent read up on polymorphism, I always see it about but can't be bothered, now I know what it is I'll have a read, thanks

Don't make war, make tea.

Login to post a reply

Server time is: 2024-09-28 14:08:49
Your offset time is: 2024-09-28 14:08:49