Re: constant static public attributes

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux