This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: constant static public attributes


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]