John Harrison wrote in news:2q******** ****@uni-berlin.de in
comp.lang.c++:
"alexwu" <al*****@nospam .com> wrote in message
news:Id******** *************@b gtnsc04-news.ops.worldn et.att.net... typedef union _MYUNION
struct {
To the OP: Anonymous struct's aren't Standard C++, you *must*
declare/define something.
short a;
short b;
};
long c;
} MYUNION;
[...]
You cannot. Unions to not have constructors and therefore cannot be
initialised; only assigned to.
sed s/Unions/Aggregate types/
I.e. array's and constructor-free struct's have the same problem.
However union's with a user defined constructor don't:
#include <iostream>
union my_union
{
int i;
double d;
my_union( int ii ) : i( ii ) {}
};
int main()
{
std::cout << my_union( 1 ).i << '\n';
}
Also you can use *value-initialization* with unions:
#include <iostream>
#include <ostream>
union my_union
{
int i;
double d;
};
struct X
{
my_union m;
X() : m() {}
};
int main()
{
std::cout << X().m.i << '\n';
}
Which should, assuming IIUC and its put through a recient Standard
conforming compiler print 0.
Rob.
--
http://www.victim-prime.dsl.pipex.com/