Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > detecting constructor called before main

Reply
Thread Tools

detecting constructor called before main

 
 
440gtx@email.com
Guest
Posts: n/a
 
      02-01-2007
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?

 
Reply With Quote
 
 
 
 
robertwessel2@yahoo.com
Guest
Posts: n/a
 
      02-01-2007
On Feb 1, 12:59 am, (E-Mail Removed) 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.

 
Reply With Quote
 
 
 
 
Alan Johnson
Guest
Posts: n/a
 
      02-01-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
main() called inside main() rahulsinner@gmail.com C Programming 14 05-07-2006 10:23 PM
What will happen if main called in side main function? Ravi C Programming 17 04-01-2006 10:08 PM
doing some initialization before the constructor gets called Vamsi C++ 3 07-19-2005 04:46 AM
how to pass this before supertype constructor has been called Paul J. Lucas Java 28 12-05-2003 09:23 PM



Advertisments