CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jul 2009
    Posts
    154

    static and constructors

    Hi,

    When is the constructor called in a situation like this?


    Code:
    class MyClass
    {
    public:
                             MyClass() { cout << "Hi" << endl; }
    
      static MyClass * GetInstance() { static MyClass myClass; return &myClass; }
    };
    Tricky one..
    Last edited by ProgrammerC++; October 3rd, 2010 at 05:38 AM.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: static and constructors

    Have you tried stepping through the code in a debugger?

    Won't that pretty much answer the question for you?

  3. #3
    Join Date
    Jul 2009
    Posts
    154

    Re: static and constructors

    No. Nevermind, I do not feel like explaining why this situation could be tricky.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: static and constructors

    To be honest, the code doesn't make a lot of sense. It's kind of like a singleton, but not really since you need an instance to access the GetInstance method.

  5. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: static and constructors

    AFAIK static globals are constructed before main() is called. I'm not sure about static locals of member functions, though.

    But in your particular case I doubt that this would compile at all: Classes are not allowed to contain members of their own type (only pointers are allowed), and I somehow suppose that this applies to static member function locals as well.

    OTOH, the rule that class members of the class' own type are not allowed is based on the fact that this would result in infinite object size. I think this would not apply to static member function locals, but I'm not sure whether the creators of the standard introduced an extra exception to the general rule for people doing daring things like you do...
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  6. #6
    Join Date
    Jul 2009
    Posts
    154

    Re: static and constructors

    A method does not contribute to the size of a class..

    It compiles, I tested and the constructor is called the first time I use GetInstance(), so not before main(), which is the weird thing, because the compiler can't know when the first call to the constructor must be made in a complex code.

    I forgot in my sample code to put 'static MyClass *' , i did that now.
    Last edited by ProgrammerC++; October 3rd, 2010 at 05:38 AM.

  7. #7
    Join Date
    Jun 2008
    Posts
    592

    Re: static and constructors

    Quote Originally Posted by ProgrammerC++
    the constructor is called the first time I use GetInstance(), so not before main(), which is the weird thing, because the compiler can't know when the first call to the constructor must be made in a complex code.
    I don't find it weird at all. the static object is confined by the scope of the static function even though the lifespan is not restricted. it would make sense for it to be constructed on first entry.

    3.3.1 Point of declaration [basic.scope.pdecl]
    1 The point of declaration for a name is immediately after its complete declarator (clause 8) and before its
    initializer (if any)
    6.7.4 The zero-initialization (8.5) of all local objects with static storage duration (3.7.1) is performed before any
    other initialization takes place. A local object of POD type (3.9) with static storage duration initialized with
    constant-expressions is initialized before its block is first entered. An implementation is permitted to perform
    early initialization of other local objects with static storage duration under the same conditions that an
    implementation is permitted to statically initialize an object with static storage duration in namespace scope
    (3.6.2). Otherwise such an object is initialized the first time control passes through its declaration; such an
    object is considered initialized upon the completion of its initialization. If the initialization exits by throwing
    an exception, the initialization is not complete, so it will be tried again the next time control enters the
    declaration. If control re-enters the declaration (recursively) while the object is being initialized, the behavior
    is undefined.
    Only global object may get initialized before main and not local static scoped objects.

    3.6.3 It is implementation-defined whether or not the dynamic initialization (8.5, 9.4, 12.1, 12.6.1) of an object of
    namespace scope is done before the first statement of main. If the initialization is deferred to some point
    in time after the first statement of main, it shall occur before the first use of any function or object defined
    in the same translation unit as the object to be initialized.
    3.3.3 The outermost declarative region of a translation unit is also a namespace, called the global namespace. A
    name declared in the global namespace has global namespace scope (also called global scope). The potential
    scope of such a name begins at its point of declaration (3.3.1) and ends at the end of the
    Hopefully this answers a few questions...
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  8. #8
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: static and constructors

    Quote Originally Posted by Joeman View Post
    Only global object may get initialized before main and not local static scoped objects.
    I never stop learning... Thanks for clarifying.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  9. #9
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: static and constructors

    to put this in 'plain code' what the compiler essentially does for you in this...

    Code:
    static MyClass * MyClass::GetInstance() 
    { 
         static MyClass myClass; 
         return &myClass; 
    }
    more or less gets turned into:

    Code:
    static byte MyClassData[sizeof(MyClass)]; 
    static bool bObjectConstructed = false;
    
    static MyClass * MyClass::GetInstance() 
    { 
         if (!bObjectConstructed)
         {
             bObjectConstructed=true;
             ((MyClass*)MyClassData)->MyClass();
         }
         return ((MyClass*)MyClassData); 
    }
    
    class DestroyObjects
    {
         ~DestroyObjects()
         {
             if (bObjectConstructed)
             {
                 ((MyClass*)MyClassData)->~MyClass();
             }
         }
    };
    
    DestroyObjects objDestroyAtExit;
    So this does what's required, the static MyClass object gets constructed at the first time the function gets called, which is how it's supposed to be.
    Last edited by OReubens; October 4th, 2010 at 07:50 AM.

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: static and constructors

    The usual complaint about the method-local static is that its construction isn't thread-safe.

    So the question arises: what about a static variable in an inner scope? Will it be constructed when it first comes into scope, in which case it could be protected, or when the function is first called, in which case it can't?

    Code:
    static MyClass & MyClass::GetInstance() 
    { 
         mutex mlock(creationmutex);
         {
             static MyClass myClass; 
             return myClass;
         } 
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured