This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: constant static public attributes
- From: Eljay Love-Jensen <eljay at adobe dot com>
- To: Bruno Schneider <bruno_schneider at hotmail dot com>, gcc-help at gcc dot gnu dot org
- Date: Mon, 27 Sep 2004 23:44:06 -0500
- Subject: Re: constant static public attributes
- References: <BAY9-F4433TmpwpcMW000016014@hotmail.com>
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