Topic: union static members
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1998/03/17 Raw View
Roman Belenov <unreal_undead@yahoo.com> writes:
>Steve Clamage wrote:------------- skipped ---------------------------
>> 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;
>> };
>>
>Well, in the second (legal) definition 't' doesn't live in the same
>place with 'a' and 'b'. I think that static union would be useful if I
>want to have one variable for all program, but address it using
>different types:
>union NullPointer
>{
> static int integer_representation;
> void* pointer;
> } localNullPointer;
>Of course, making one member static should automatically make other
>members static.
I don't understand what you are trying to accomplish with a static
member of a union. I was assuming that such a thing would behave
the same as a static member of a class. Maybe you had some other
behavior in mind.
If you want to store objects of different types at the same address,
you can use a C-like or an anonymous union.
If you want to create a single variable which can hold values
of different types, you can use a regular union, an anonymous union,
possibly in a namespace. Example:
union { // regular union of a unique type
int a;
double b;
char* c;
} M;
Objects M.a, M.b, and M.c are all at the same address. There is
only one (of each). If the declaration is at file (or namespace)
scope, they are all static. Since the union has no tag, you cannot
declare another union object with the same type, although you could
create another type with the same layout. (My earlier example
defined a type so that you could declare other objects of that type.)
namespace N {
union { // anonymous union in a namespace
int a;
double b;
char* c;
};
}
Objects N::a, N::b, and N::c are all at the same address. There is
only one (of each). If the declaration is at file (or namespace)
scope they are all static.
--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Roman Belenov <unreal_undead@yahoo.com>
Date: 1998/03/06 Raw View
Steve Clamage wrote:------------- skipped ---------------------------
> 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;
> };
>
Well, in the second (legal) definition 't' doesn't live in the same
place with 'a' and 'b'. I think that static union would be useful if I
want to have one variable for all program, but address it using
different types:
union NullPointer
{
static int integer_representation;
void* pointer;
} localNullPointer;
Of course, making one member static should automatically make other
members static.
Why does C++ not permit this ?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Christopher M. Gurnee" <gurnec@nospam.yahoo.com>
Date: 1998/03/06 Raw View
Roman Belenov wrote in message <34FEC435.52260C7E@yahoo.com>...
<snip>
>Well, in the second (legal) definition 't' doesn't live in the same
>place with 'a' and 'b'. I think that static union would be useful if
I
>want to have one variable for all program, but address it using
>different types:
>
>union NullPointer
>{
> static int integer_representation;
> void* pointer;
> } localNullPointer;
>
>Of course, making one member static should automatically make other
>members static.
>Why does C++ not permit this ?
It does, just not with that syntax. I think you meant something like:
(in some header file):
union NullPointer {
int int_rep;
void* pointer;
};
extern NullPointer globalNullPointer;
(in some source file that includes the above):
// the global NullPointer union
NullPointer globalNullPointer;
// a class containing a static union
class A {
// . . .
static NullPointer localNullPointer;
// . . .
};
Be aware that it isn't technically safe to assume that after setting
one member, the other contains the reinterpret_cast of the first (even
though it probably will on implementations where sizeof(void*) ==
sizeof(int)).
-Chris
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1998/03/06 Raw View
Roman Belenov wrote:
[...]
> Well, in the second (legal) definition 't' doesn't live in the same
> place with 'a' and 'b'. I think that static union would be useful if I
> want to have one variable for all program, but address it using
> different types:
>
> union NullPointer
> {
> static int integer_representation;
> void* pointer;
> } localNullPointer;
>
> Of course, making one member static should automatically make other
> members static.
> Why does C++ not permit this ?
Shouldn't
static union NullPointer
{
int integer_representation;
void* pointer;
} localNullPointer;
do what you want?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: David R Tribble <david.tribble@central.beasys.com>
Date: 1998/03/04 Raw View
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?
-- David R. Tribble, david.tribble@noSPAM.central.beasys.com --
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1998/03/04 Raw View
In article 2424@central.beasys.com, David R Tribble <david.tribble@central.beasys.com> writes:
>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?
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";
---
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]