How can I control static object construction order? 
Author Message
 How can I control static object construction order?

I have a group of static (global) objects one of which is a low-level
utility object.  Several of the static objects' constructors use the
services provided by the utility object, so the utility object must be
completely constructed before any of the other static objects
constructors run.

Does Visual C++ provide any mechanism for controlling the construction
order of global objects?  I know Borland provides a pragma "startup"
that lets you hook a function of your own into the startup sequence at
a specified priority.  Other mature compilers have similar pragmas, but
I haven't been able to find anything in VC++ that serves this purpose.
Any hints?

--
Chris Hamilton
Wayne/Dresser, Austin, TX
chamilton(at)wayne.com

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Sun, 24 Mar 2002 03:00:00 GMT  
 How can I control static object construction order?
There is a standard way to do this - no pragmas required.

Instead of:

MyClass GlobalObject;

have:

MyClass& GlobalObject()
{
    static MyClass sGlobalObject;
    return sGlobalObject;

Quote:
}

sGlobalObject will get constructed when it is first needed. Think
of it as just in time construction.

Quote:

> I have a group of static (global) objects one of which is a low-level
> utility object.  Several of the static objects' constructors use the
> services provided by the utility object, so the utility object must be
> completely constructed before any of the other static objects
> constructors run.

> Does Visual C++ provide any mechanism for controlling the construction
> order of global objects?  I know Borland provides a pragma "startup"
> that lets you hook a function of your own into the startup sequence at
> a specified priority.  Other mature compilers have similar pragmas, but
> I haven't been able to find anything in VC++ that serves this purpose.
> Any hints?

> --
> Chris Hamilton
> Wayne/Dresser, Austin, TX
> chamilton(at)wayne.com

> Sent via Deja.com http://www.deja.com/
> Before you buy.

--
.Bruce Dawson, Cavedog Entertainment.
Makers of Total Annihilation - http://www.cavedog.com


Sun, 24 Mar 2002 03:00:00 GMT  
 How can I control static object construction order?
Awesome.  Just awesome.  I'll try that and let you know if it works.
It certainly appears to be dead on the money!


Quote:

> There is a standard way to do this - no pragmas required.

> Instead of:

> MyClass GlobalObject;

> have:

> MyClass& GlobalObject()
> {
>     static MyClass sGlobalObject;
>     return sGlobalObject;
> }

> sGlobalObject will get constructed when it is first needed. Think
> of it as just in time construction.


> > I have a group of static (global) objects one of which is a low-
level
> > utility object.  Several of the static objects' constructors use the
> > services provided by the utility object, so the utility object must
be
> > completely constructed before any of the other static objects
> > constructors run.

> > Does Visual C++ provide any mechanism for controlling the
construction
> > order of global objects?  I know Borland provides a pragma "startup"
> > that lets you hook a function of your own into the startup sequence
at
> > a specified priority.  Other mature compilers have similar pragmas,
but
> > I haven't been able to find anything in VC++ that serves this
purpose.
> > Any hints?

> > --
> > Chris Hamilton
> > Wayne/Dresser, Austin, TX
> > chamilton(at)wayne.com

> > Sent via Deja.com http://www.deja.com/
> > Before you buy.

> --
> .Bruce Dawson, Cavedog Entertainment.
> Makers of Total Annihilation - http://www.cavedog.com

--
Chris Hamilton
Wayne/Dresser, Austin, TX
chamilton(at)wayne.com

Sent via Deja.com http://www.deja.com/
Before you buy.



Mon, 25 Mar 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Order of construction of static class members ?

2. Construction order of members/global objects

3. Construction of local static objects in a multithreaded environment

4. Question about: FIX: Visual C++ Class Destructors Are Not Called in Reverse Order of Construction

5. static construction clause

6. Object Construction

7. Static array containing non static objects and functions

8. Static Library static object initialization

9. Getting called at the end of construction with an ATL ActiveX control

10. Z -order between contained controls inside a composite control

11. C++ static class members destruction order.

12. Order of static member func execution before main()

 

 
Powered by phpBB® Forum Software