Single instance

Discussion in 'C++' started by ccs, Jun 6, 2004.

  1. ccs

    ccs Guest

    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"?
    ccs, Jun 6, 2004
    #1
    1. Advertising

  2. ccs

    Phlip Guest

    ccs wrote:

    > 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?

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
    Phlip, Jun 6, 2004
    #2
    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. Randall Parker
    Replies:
    1
    Views:
    582
    S. Justin Gengo
    Dec 12, 2005
  2. Chris
    Replies:
    1
    Views:
    13,589
    Oisin
    Mar 24, 2006
  3. chris brat
    Replies:
    1
    Views:
    609
    chris brat
    May 10, 2006
  4. Eric Layman
    Replies:
    3
    Views:
    606
    Rad [Visual C# MVP]
    Apr 14, 2007
  5. goodfella

    single producer, single consumer

    goodfella, Oct 6, 2009, in forum: C++
    Replies:
    41
    Views:
    1,906
    James Kanze
    Oct 12, 2009
Loading...

Share This Page