detecting constructor called before main

Discussion in 'C++' started by 440gtx@email.com, Feb 1, 2007.

  1. Guest

    I have a class that maintains a static std::list of its instances.
    Thus, due to the order of constructors ambiguity of itself and
    std::list, it cannot be safely instantiated until after main is
    called. What I would like to do is put in an assert if it is
    instantiated too early, but unsure how to detect this case. Or perhaps
    there is a better design for instance tracking?
    , Feb 1, 2007
    #1
    1. Advertising

  2. Guest

    On Feb 1, 12:59 am, wrote:
    > I have a class that maintains a static std::list of its instances.
    > Thus, due to the order of constructors ambiguity of itself and
    > std::list, it cannot be safely instantiated until after main is
    > called. What I would like to do is put in an assert if it is
    > instantiated too early, but unsure how to detect this case. Or perhaps
    > there is a better design for instance tracking?



    Set a global flag (that's initialized not-set) at the top of main().
    Test it in the ctor in question. Note that this will run *after* any
    ctors or initializers called for automatics as main is entered.
    , Feb 1, 2007
    #2
    1. Advertising

  3. Alan Johnson Guest

    wrote:
    > I have a class that maintains a static std::list of its instances.
    > Thus, due to the order of constructors ambiguity of itself and
    > std::list, it cannot be safely instantiated until after main is
    > called. What I would like to do is put in an assert if it is
    > instantiated too early, but unsure how to detect this case. Or perhaps
    > there is a better design for instance tracking?
    >


    Instead of declaring the list as a static member of the class, declare
    it as a local static variable in a static function, like so:

    class A
    {
    static std::list<A *> & get_list() ;

    A()
    {
    get_list().push_back(this) ;
    }
    } ;

    std::list<A *> & A::get_list()
    {
    static std::list<A *> instances ;
    return instances ;
    }

    You are guaranteed that 'instances' will be constructed the first time
    control passes over it, which should eliminate your problem.

    --
    Alan Johnson
    Alan Johnson, Feb 1, 2007
    #3
    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. Paul J. Lucas
    Replies:
    28
    Views:
    6,158
    Dale King
    Dec 5, 2003
  2. Ravi
    Replies:
    17
    Views:
    924
    Kenneth Brody
    Apr 1, 2006
  3. main() called inside main()

    , May 4, 2006, in forum: C Programming
    Replies:
    14
    Views:
    1,426
    Richard Heathfield
    May 7, 2006
  4. Vamsi
    Replies:
    3
    Views:
    410
    AbdulMunaf
    Jul 19, 2005
  5. Boris
    Replies:
    6
    Views:
    1,364
    Patricia Shanahan
    Apr 21, 2007
Loading...

Share This Page