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 / Polymorphism or Something Else?

Author
Message
kklouzal
15
Years of Service
User Offline
Joined: 15th May 2009
Location: Arizona
Posted: 11th Mar 2010 20:44 Edited at: 11th Mar 2010 20:46
I want to create a single class, Tool:

And only create one instance of this class 'myTool'


Then I want to be able to create multiple new classes with this general form:
Tool1:

Tool2:

The only aspect of what will be different in these classes are the code that is actually in the 'PrimaryAction' functions.

Now, I want to be able to call the function Tool.PrimaryTrigger() and have it the ability to see all the classes I created for the different tools and if the bool Selected == true then have it run the PrimaryAction() function of that 'sub class'.

So I want to create multiple 'sub classes' that are controlled from the 'base class', is this possible? if so point me in the proper direction for me to start learning! tyvm

Add me to your MSN!
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 11th Mar 2010 22:25
Polymorphism:


Hopefully I've added enough comments to make it understandable.

Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 11th Mar 2010 23:17 Edited at: 11th Mar 2010 23:20
Maybe it would have been good to describe what purpose you want to achieve with this, because I'm not convinced it has to be that complicated. When I read your description twice, I realized that it is not even evident whether the tools must be really derived from the first tool class, as IanM supposed in his very nice example above, or they are only controlled by it but not derived from it? Note that, if they are derived, then all the classes inherit the PrimaryTrigger function, so the behaviour is controlled not because the base class "sees" the others, but because the base behaviour is present in all of them. Maybe it would be better to separate inheritance of behaviour and control of behaviour.

If only the behaviour of the tools is different, then it is indeed a good case for polymorphysm with a base class, but to invoke that behaviour, an array should do the job. (You can incapsulate that array into a controller or "container" class if you want, but that would be a different class from the base tool class.) This page shows a good example of what I mean:

http://en.wikipedia.org/wiki/Virtual_function
kklouzal
15
Years of Service
User Offline
Joined: 15th May 2009
Location: Arizona
Posted: 11th Mar 2010 23:28
You both provided very good examples, thank you much it has helped a great deal.

Add me to your MSN!
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 12th Mar 2010 20:25 Edited at: 12th Mar 2010 20:25
@Mireben,
You're right. I assumed a lot, and even missed a bit:
Quote: "have it the ability to see all the classes I created for the different tools"


That sounds like every time you create a SuperSuckyTool or SuperAwesomeTool that it needs to be recorded by the one and only Tool object so that each can be called when Tool::[/b]PrimaryTrigger is invoked. In this instance, the subclasses don't need to inherit from Tool, but do need a common base class so that Tool can store pointers to the other class objects.

That sounds a lot like this: http://www.boost.org/doc/libs/1_42_0/doc/html/signals.html

kklouzal
15
Years of Service
User Offline
Joined: 15th May 2009
Location: Arizona
Posted: 12th Mar 2010 22:25
I was able to get done what was needed with your example, I thank you very much!

Add me to your MSN!

Login to post a reply

Server time is: 2024-10-02 01:38:47
Your offset time is: 2024-10-02 01:38:47