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

Discussion in 'C++' started by ardi, May 19, 2013.

  1. ardi

    ardi Guest

    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
    ardi, May 19, 2013
    #1
    1. Advertising

  2. ardi

    Rui Maciel Guest

    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
    Rui Maciel, May 19, 2013
    #2
    1. Advertising

  3. ardi

    ardi Guest

    On Sunday, May 19, 2013 7:19:13 PM UTC+2, Rui Maciel wrote:
    > 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?
    >


    Wow!!! Didn't know such thing existed!! Thanks a lot, this is exactly what I needed!

    ardi
    ardi, May 19, 2013
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DJ Dev
    Replies:
    3
    Views:
    16,117
    Gandalf
    Feb 8, 2004
  2. E11
    Replies:
    1
    Views:
    4,742
    Thomas Weidenfeller
    Oct 12, 2005
  3. John Wohlbier
    Replies:
    2
    Views:
    366
    Josiah Carlson
    Feb 22, 2004
  4. Replies:
    6
    Views:
    321
    Victor Bazarov
    Aug 13, 2005
  5. markww
    Replies:
    5
    Views:
    342
    markww
    Aug 25, 2006
Loading...

Share This Page