Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
* * * * * 1 votes

Polymorphism?

polymorphism

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1 Kyle Joseph Klouzal

Kyle Joseph Klouzal

    CC Newcomer

  • New Member
  • PipPip
  • 14 posts

Posted 23 November 2012 - 09:51 PM

I'm going to give a description of what i'd like to accomplish and would like to know if it describes polymorphism or something else, possibly even something which is beyond c++ I'm not quite sure.

Say you have this simple class:
class Vars
{
public:
	int a,b,c;
};
which would hold some...global variables...

And you have these other two simple classes:
class SomeObject : public Vars
{
public:
	int x;
};

class SomeOtherObject : public Vars
{
public:
	int y;
};
which in theory could be anything from npcs, players, weapons, etc.

And we create some instances of these objects as such:
Vars AppVars;
SomeObject objA;
SomeObject objB;
SomeObject objC;
SomeOtherObject objX;
SomeOtherObject objY;
SomeOtherObject objZ;
I create ONE instance of Vars AppVars and set it's variables.
Three instances of SomeObject and SomeOtherObject, okay, good to go.

Now onto my question; Is there any way to 'bind' objA-C & objX-Y to AppVars so they 'share' the value of all variables within AppVars?
For the main reason of doing this.
Changing the value of any variable in the base class Vars through the subclass SomeObject or SomeOtherObject would respectively update AppVars and all other child class object instances?

e.g. changing objA.a = 3 would ALSO update objB.a to 3, objC.a to 3, objX.a to 3, objY.x to 3, objZ.x to 3, aswell as .b and .c through all derived objects?


What am I trying to do here and is this even possible??
This would open up many doors and windows in my code :)
As always the community's input and help is most appreciated.
This has been an issue that's plagued me from the start of my c++ ventures >.<

#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 24 November 2012 - 07:16 AM

For that to happen, one way would be vars's int variables to be pointers or references, and assigning all of them to global variables that actually are meant to keep the up to date values.

So is up to you to work some magic in the vars's constructor ;)

Totally doable.

#3 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts

Posted 24 November 2012 - 10:08 AM

Protected/public static variables should do the trick.

The roots of education are bitter, but the fruit is sweet.


#4 0xDEADBEEF

0xDEADBEEF

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 790 posts

Posted 25 November 2012 - 11:23 AM

I think there are a number of ways you could achieve this.

you could do it via public inhiretance where the base class has static variables, as suggested by the Flying Dutchman. If you wished to have more than one set of vars then you can have the base class have pointers to an implementation e.g.

class Base
{
public:
Base() { a_ = new int; b_ = new int; owned_ = true; }
Base(const Base& rhs) { a_ = rhs.a_; b_ = rhs.b_ }

int *a_, *b_;
};

class A : Base {
    A() : Base() { }
    A(const Base& rhs) : Base(rhs) { }
};

The problem with this is that you're binding to variables based on the structure of the class; which isn't to flexible; but still allow different bases. Although you need to careful with the memory usage, if you destroy a shared base then you're cause errors in the subclasses which are bound. You could solve that with reference counting for instance.

Another option would be use functions instead of exposed properties (a la Java.) e.g.
class A {
public:
	 getA() { return varObj->a; }
};

A good compiler should be able to replace getA() with A::varObj->a. So it's pretty efficient. Again you can't destroy the vars until you destory all the subclasses.

Another option would be to use a proxy class. This would give you property syntax, but allow you to bind the vars individually, in the base class define your variables as a proxy class - this has a private pointer to the variable to bind and a conversion to the correct type. Then you define a function "bind" which takes any variable of the correct type and the proxy class and assignes the var to the proxy class. I can give you an example if you wish (I just need to brush up on my syntax)

Creating SEGFAULTs since 1995.





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download