Re: Shared globals between two classes

From: Anthony Borla (ajborla_at_bigpond.com)
Date: 11/17/03

  • Next message: Mike Wahler: "Re: Tokenizing a String"
    Date: Mon, 17 Nov 2003 20:18:38 GMT
    
    

    "ma740988" <ma740988@pegasus.cc.ucf.edu> wrote in message
    news:a5ae824.0311170521.1ac58883@posting.google.com...
    > One minor confusion. I'm trying to figure out a way to make the data
    > members (for instance tx_count, rx_count and MY_STRUCT) in Singleton
    > private. FOO_WORKER will change them. I realize that public data
    > members is - for the most part 'bad' news even though the singleton
    > approach guarantees only one instance of the class.
    >

    Little change needed except for marking them 'private' and providing
    suitable accessor and mutator member functions. A simple example that
    follows the outline of my earlier-posted example:

    class COMMON
    {
       ...
       // Accessors
       int getTXCount() const { return tx_count; }
       int getRXCount() const { return rx_count; }
       ...
       // Mutators
       void setTXCount(int tx_c) { tx_count = tx_c; }
       void setRXCount(int rx_c) { rx_count = rx_c; }
       ...
    private:
       // Private [instance] data
       int tx_count;
       int rx_count;
       ...
    };

    class FOO_WORKER
    {
       ...
       // Access via passed pointer
       void changeCOMMONData(COMMON* cInst)
       {
          ...
          cInst->setTXCount(0);
          cInst->setRXCount(0);
          ...
          int tx = cInst->getTXCount();
          int rx = cInst->getRXCount();
          ...
       }
       ...
       // Access via passed pointer
       void anotherChangeCOMMONData()
       {
          ...
          COMMON* cInst = COMMON::getInstance();

          cInst->setTXCount(0);
          cInst->setRXCount(0);
          ...
       }
    };

    int main()
    {
        ...
        COMMON* cInst = COMMON::getInstance();
        ...
        FOO_WORKER fw;
        ...
        fw.changeCOMMONData(cInst);
        ...
        fw.anotherChangeCOMMONData();
        ...
    }

    I recognise the Singleton structure may be somewhat confusing but please
    realise that:

    * It is an object of the singleton type that exists and is being
       accessed via the static 'getInstance()' member function

    * The singleton contains instance application data, not static
       application data. The only static components are those
       holding and allowing access to, the single instance object

    Hence, aside from the 'singleton scaffolding' code, there need be little
    difference between the singleton object and other 'typical' objects - it
    would be structured similarly to these:

       class TypicalClass
       {
       public:
          // Constructor(s)
          ...
          // Accessors
          ...
          // Mutators
          ...
       private:
          // Instance data
          ...
       };

    I hope this is reasonably clear to you.

    I hope this helps.

    Anthony Borla


  • Next message: Mike Wahler: "Re: Tokenizing a String"

    Relevant Pages

    • Re: Shared globals between two classes
      ... I realize that public data ... Little change needed except for marking them 'private' and providing ... int getTXCount() const ... I recognise the Singleton structure may be somewhat confusing but please ...
      (alt.comp.lang.learn.c-cpp)
    • Re: singleton
      ... >> I don't believe this is a singleton. ... static int count; ... static int getCount ...
      (comp.lang.cpp)
    • poker odds
      ... public static final int HIGH_CARD = 0; ... private static Logger logger; ... public Poker(int numCard, Card deck) ... public void maxSimulation ...
      (comp.lang.java.programmer)
    • problem with vmr9 image compositor - method Draw
      ... I play video with playlist, if i play video about 5x it is ... but so 6round of video playing throw exception int ... private void ConfigureVMR9InWindowlessMode() ... Surface renderTarget = new Surface; ...
      (microsoft.public.win32.programmer.directx.managed)
    • Re: using ref keyword performance
      ... // private fields... ... int valueCount = values.Length; ... static void TestWithRefLoop() ...
      (microsoft.public.dotnet.languages.csharp)