Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Shared value for a member in all instances of a class?

26 views
Skip to first unread message

ardi

unread,
May 19, 2013, 1:05:05 PM5/19/13
to
Hi again,

I've some classes with members that need to have a pointer to a GUI control.

However, the same member will have the same pointer in all instances of the class. I mean, member 'm_Temperature' from class 'CTown' will have the same value for its GUI control pointer in all instances of the class 'CTown'.

Thus, if I've 50000 objects of class CTown, I'm wasting a lot of unneeded pointers, since all of them have the same value.

My first idea is that I need another class, like for example CTownGUI, which stores the GUI pointers, so that CTown doesn't have to waste memory with them.

But, unfortunately, this makes the code harder to write: Everytime I add a new member in CTown, I've to add its counterpart to CTownGUI, and I need to code its GUI pointer management.

On the other hand, if I was OK with wasting memory, with the first approach I wouldn't need any extra coding when adding a new member to CTown: if the GUI pointer of the new member is managed on its base class, just add the new member to CTown, and everything works... well, works, but wasting a lot of pointers that have the same value.

So, do you have some clever idea which would allow all instances of a class to share the same memory location for a member?

Thanks!

ardi

Rui Maciel

unread,
May 19, 2013, 1:19:13 PM5/19/13
to
ardi wrote:

> So, do you have some clever idea which would allow all instances of a
> class to share the same memory location for a member?

How about using a static member variable?


Rui Maciel

ardi

unread,
May 19, 2013, 1:39:26 PM5/19/13
to
Wow!!! Didn't know such thing existed!! Thanks a lot, this is exactly what I needed!

ardi


0 new messages