On Wednesday 08 September 2004 03:31, John Lamb wrote:
Steven T. Hatton wrote:
I've seen this kind of problem before. Effectively, you're trying to initialise a static const class member at runtime (i.e. not compile time!) in a static object That doesn't work, though I can't remember why - I don't know if it should work in C++ or if it's implementation defined.
No, I am specifically trying to initialize it at compile time. What the stupid compiler or linker is trying to do is a different story! ;) The code works if I build all the parts in the same compilation unit.
There are several solutions. First, I think the code will work if you create a shared library. Similar code does for me.
That might be the answer. I haven't attempted that yet.
Perhaps a neater solution for you would be to replace the static const variables with static const functions of the form:
const util::RgbColor util::RgbColor::RED(){ static util::RgbColor util::RgbColor RED = util::RgbColor(255,0,0); return RED; }
That should work unless you're running the code in an insanely multithreaded environment.
For all that, I could probably use a non-constant private static member, have some kind of first load static initialization flag, and only allow access through const functions. But, damnit! The code I posted is correct! I'll see if I get any feedback from the gnu folks explaining what's going on. I wonder if playing around with extern might work? -- Regards, Steven