473,320 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

using a union in an initialization list

I just realized that I cant use a union in an initialization list (or
at least not the way I tried to use it).

This is what I did:

class ValueObject
{
public:
ValueObject();
ValueObject(const ValueObject&);
ValueObject& operatr= (const ValueObject&);

private:
union Item { int i, double d, char s[8]} m_item ;
enum { integer, Double, String } m_type ;
};
//impl:

ValueObject():m_type(Integer), m_item.i(0) //<- compiler complains
{}

ValueObject():m_type(Integer)//<- compiler ok with this - why?
{m_item.i(0);}

Oct 3 '08 #1
2 3526
On Oct 3, 1:38 pm, Bit Byter <takashi_...@hotmail.comwrote:
I just realized that I cant use a union in an initialization list (or
at least not the way I tried to use it).

This is what I did:

class ValueObject
{
public:
ValueObject();
ValueObject(const ValueObject&);
ValueObject& operatr= (const ValueObject&);

private:
union Item { int i, double d, char s[8]} m_item ;
enum { integer, Double, String } m_type ;

};

//impl:

ValueObject():m_type(Integer), m_item.i(0) //<- compiler complains
{}

ValueObject():m_type(Integer)//<- compiler ok with this - why?
{m_item.i(0);}

//Corrections:
1. assignment operator misspelled should be 'operator='
2. ctor statement should read: m_item.i = 0;
Oct 3 '08 #2
On Oct 3, 2:38 pm, Bit Byter <takashi_...@hotmail.comwrote:
I just realized that I cant use a union in an initialization
list (or at least not the way I tried to use it).
This is what I did:
class ValueObject
{
public:
ValueObject();
ValueObject(const ValueObject&);
ValueObject& operatr= (const ValueObject&);
private:
union Item { int i, double d, char s[8]} m_item ;
enum { integer, Double, String } m_type ;
};
//impl:
ValueObject():m_type(Integer), m_item.i(0) //<- compiler complains
{}
That's because the syntax doesn't allow it. I suppose it could,
in the case where the element was an agglomerate with a trivial
constructor, but the language standard doesn't provide for it.
ValueObject():m_type(Integer)//<- compiler ok with this - why?
{m_item.i(0);}
The line in your constructor body is legal? I should only be
legal if m_item.i were a function. In the constructor body, you
have to use assignment.

There are two possible solutions. The simplest is just use
assignment. All union members are required to have trivial
constructors, so there is no difference between initialization
and assignment. Otherwise, you can provide the union with a
constructor, e.g.:

union Item
{
int i ;
double d ;
char s[ 8 ] ;
Item( int i ) : i( i ) {}
Item( double d ) : d( d ) {}
// ...
} ;

Or unless there are literally millions of these objects, just
use a struct. (That's what I did in a similar case.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 4 '08 #3

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

Similar topics

6
by: Rudolf Bargholz | last post by:
Hi , I have the following tables ------------- PAX: Id Order_Id Name Position
6
by: Neil Zanella | last post by:
Hello, I would like to know what the C standards (and in particular the C99 standard) have to say about union initializers with regards to the following code snippet (which compiles fine under...
13
by: Kantha | last post by:
Hi all, I have declared an Union as follows typedef union { struct interrupt_bits { unsigned char c_int_hs_fs_status : 1, c_setup_intflag : 1,
15
by: Dilip | last post by:
The subject is a bit misleading because I really can't figure out what the following code snippet is doing and would appreciate any help in deciphering it. I mean I can understand code-wise what...
5
by: Richard Gromstein | last post by:
Hello, I have an exercise that I need to finish very soon and I really need help understanding what to do and how exactly to do it. I am working on reading the chapter right now and working on it...
8
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;
4
by: Theo R. | last post by:
Hi all, I have the following struct defined - #define INTEGER 0 #define STRING 1 typedef struct { char type ; union {
4
by: benn686 | last post by:
I have a structure that contains a union that Id like to initialize at compile time... something like: //global declare and initialize fullStructType var1 = { unionMember.union1.field1...
11
by: pereges | last post by:
Hello, can some one please guide me a little into using unions. I read about unions in K & R but I am finding it difficult to apply to my problem at hand. I want to save up some space by using...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.