Single instance

C

ccs

class csingle
{
public:
static csingle membs;
int n1, n2, n3;
csingle(int v1, int v2, int v3)
{
...
}
....
};

csingle csingle::membs(5, 7, 13);

Is there anything wrong with the code for creating a single instance of
csingle?

How to get the address of the object "csingle::membs"?
 
P

Phlip

ccs said:
class csingle
{
public:
static csingle membs;
int n1, n2, n3;
csingle(int v1, int v2, int v3)
{
...
}
...
};

csingle csingle::membs(5, 7, 13);

Is there anything wrong with the code for creating a single instance of
csingle?

Look up the Singleton Pattern in the book /Design Patterns/. Then don't
abuse it.

You have reinvented much of it, but there's one small problem with your
code. If you use csingle::membs before main() calls, (such as from the
constructor of another global object in another translation unit
["module"]), you might accidentally access that object before it constructs.
How to get the address of the object "csingle::membs"?

Uh, &csingle::membs?
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top