constant static public attributes

Eljay Love-Jensen eljay@adobe.com
Tue Sep 28 04:44:00 GMT 2004


Hi Bruno,

That's the classic "Order of Construction" problem.

To get around it, use this trick:
-- color.h ---
class color {
[...]
    static color const& WHITE();
}
------
#include "color.h"
-- color.cpp ---
color const& color::WHITE()
{
   static color const rv(255,255,255);
   return rv;
}
[...]
------
color.o is generated by "g++ -o color.o -c color.cpp"
-- light.h ---
#include "color.h"
class light {
[...]
   static light const& getSUN();
}
------
-- light.cpp ---
#include "light.h"
light const& light::SUN()
{
   // the color of SUN is WHITE
   static light const rv(..., color::WHITE())
   return rv;
}
[...]
------

If you want to hide the function, don't use a macro.  Keep the static 
accessor functions (but maybe change their names), and make a static const 
class variable (like you had before), but make sure the static const class 
variables are initialized by the appropriate static accessor functions (not 
by references to the perhaps-not-yet-initialized external variables).  Then 
everything should work okay.

HTH,
--Eljay



More information about the Gcc-help mailing list