Re: Shared globals between two classes
From: Anthony Borla (ajborla_at_bigpond.com)
Date: 11/17/03
- Previous message: Jenski182: "Tokenizing a String"
- In reply to: ma740988: "Re: Shared globals between two classes"
- Next in thread: B. v Ingen Schenau: "Re: Shared globals between two classes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Previous message: Jenski182: "Tokenizing a String"
- In reply to: ma740988: "Re: Shared globals between two classes"
- Next in thread: B. v Ingen Schenau: "Re: Shared globals between two classes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|