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


A

ardi

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 theclass. 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 tocode its GUI pointer management.

On the other hand, if I was OK with wasting memory, with the first approachI wouldn't need any extra coding when adding a new member to CTown: if theGUI 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 classto share the same memory location for a member?

Thanks!

ardi
 
Ad

Advertisements

R

Rui Maciel

ardi said:
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
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top