473,804 Members | 3,373 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to initialize a member of union type

typedef union _MYUNION

struct {
short a;
short b;
};
long c;
} MYUNION;
class myclass
{
MYUNION myu;
public:
myclass();
};
// How to initialize myu in the constructor initializer list
myclass::myclas s()
{
....
}
Jul 22 '05 #1
2 8955

"alexwu" <al*****@nospam .com> wrote in message
news:Id******** *************@b gtnsc04-news.ops.worldn et.att.net...
typedef union _MYUNION

struct {
short a;
short b;
};
long c;
} MYUNION;
class myclass
{
MYUNION myu;
public:
myclass();
};
// How to initialize myu in the constructor initializer list
myclass::myclas s()
{
...
}


You cannot. Unions to not have constructors and therefore cannot be
initialised; only assigned to.

john
Jul 22 '05 #2
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/
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2038
by: Default User | last post by:
After a discussion on c.l.c++ with Ron Natalie, I'm now unclear as to the status of reading data stored as one union member, then accessed with a different one, in C99. I don't have the latest standard, as I don't work in it currently, so I use the draft standard. My reading was that this is implementation-defined, based on this: 6.5.2.2
5
2405
by: Mars | last post by:
If I want to initialize elements in an array (to a value), is using for loops the only method?? Is there any specific functions to do that??
8
5665
by: wkaras | last post by:
In my compiler, the following code generates an error: union U { int i; double d; }; U u; int *ip = &u.i; U *up = static_cast<U *>(ip); // error I have to change the cast to reinterpret_cast for the code
15
26439
by: thinktwice | last post by:
char a = { 0 } is it ok?
3
3892
by: Hallvard B Furuseth | last post by:
to find the required alignment of a struct, I've used #include <stddef.h> struct Align_helper { char dummy; struct S align; }; enum { S_alignment = offsetof(struct Align_helper, align) };
84
15910
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
8
2324
by: irwishlaw | last post by:
After compiling and running the following program #include <stdio.h> #include <string.h> typedef union { int a; char b; float c;
18
9129
by: Ehud Shapira | last post by:
Is it possible to have a declaration of a struct pointer initialized to an unnamed struct? (I'm only concerned with static/global variables, if it matters.) I'm trying to do something like: struct st_a { int i, j; };
7
6425
by: Peter Olcott | last post by:
Why can a union have a member with a copy constructor?
0
9576
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10567
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10074
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9138
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7613
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5515
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4291
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.