TITLE: static members in unions (Newsgroups: comp.std.c++, 5 Mar 98) TRIBBLE: David R Tribble >Stroustrup states in the C++PL3, section 10.4.12, that unions can >have member functions, but cannot have static members. Why? >I can't think of a good reason why this limitation exists (and >unfortunately, Bjarne doesn't give an example or reason). >Does the CD2 also have this constraint? CLAMAGE: clamage@Eng.Sun.COM (Steve Clamage) Yes. Unions in C++ are essentially the same as unions in C. The ONLY permitted features unique to C++ are non-virtual member functions and protection specifiers (public, private, protected). But an anonymous union cannot have any of those either. Allowing static members in unions would violate the basic idea of a union: each member has the same address and only one member can be active at any one time. It also doesn't seem to be necessary. You can get the same effect, as far as I can tell, by putting an anonymous union in a class. union myunion { // illegal int a; double b; static const char* t; }; class myunion { // OK, same effect union { int a; double b: }; static const char* t; }; myunion u; u.a = 1; myunion::t = "hello";