473,385 Members | 1,320 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,385 software developers and data experts.

using union keywords

Hello,

I'm trying to make a class like below...

class myClass {
public:
// ctor / dtor
....

// methods
....

public:
// public data member
union {
double data[3];
struct { double A, B, C; };
}
private:
// private data member
...
};
I found that this data member declaration is illegal in ANSI C++ but
legal in VC.

Do you have any idea in using union keyword to data[3] and struct {}
legally in ANSI C++?

this layout is just for using the data members like below

myClass my;
my.A = SOME_DOUBLE_PRECISION_VALUE;

not using
my.data[0] = ... ; // same my.A

Mar 2 '07 #1
6 2565
steve.kim wrote:
Hello,

I'm trying to make a class like below...

class myClass {
public:
// ctor / dtor
....

// methods
....

public:
// public data member
union {
double data[3];
struct { double A, B, C; };
}
private:
// private data member
...
};
I found that this data member declaration is illegal in ANSI C++ but
legal in VC.
Well, there are no anonymous structs in C++.
Do you have any idea in using union keyword to data[3] and struct {}
legally in ANSI C++?
Give the struct member a name.

Mar 2 '07 #2
On 2 Mar, 08:49, "steve.kim" <precisionma...@gmail.comwrote:
Hello,

I'm trying to make a class like below...

class myClass {
public:
// ctor / dtor
....

// methods
....

public:
// public data member
union {
double data[3];
struct { double A, B, C; };
}
private:
// private data member
...

};

I found that this data member declaration is illegal in ANSI C++ but
legal in VC.
Two things:
1. You haven't given the struct type a name. An anonymous struct is
simply wrong.
2. Even if you correct that, anonymous unions are not allowed to
contain nested types.
Do you have any idea in using union keyword to data[3] and struct {}
legally in ANSI C++?
Would this work for you? I've not executed any code using it, but it
compiles with Comeau online:

class myClass {
public:
// public data member
struct myStruct { double A, B, C; };
union {
double data[3];
myStruct s;
};
};
this layout is just for using the data members like below

myClass my;
my.A = SOME_DOUBLE_PRECISION_VALUE;

not using
my.data[0] = ... ; // same my.A
Now my.s.A is the same thing as my.data[0]

Gavin Deane
Mar 2 '07 #3
thanks for the response.

I found that my union declaration is only legal with MSVC++ extension.

this strange compiler accepts the codes like below -_-;;

struct s {
int a;
struct { int b, c, d;};
};

without MSVC++ extensions the compiler never accept the codes.

as well as my union declaration.

I think there is two C++... ANSI C++ and MSVC++.

I realized that something wrong in standards is not wrong in my
Windows platform.

thanks

steve from KR

Mar 2 '07 #4
On 2 Mar, 10:30, "steve.kim" <precisionma...@gmail.comwrote:
thanks for the response.

I found that my union declaration is only legal with MSVC++ extension.

this strange compiler accepts the codes like below -_-;;

struct s {
int a;
struct { int b, c, d;};

};

without MSVC++ extensions the compiler never accept the codes.

as well as my union declaration.

I think there is two C++... ANSI C++ and MSVC++.

I realized that something wrong in standards is not wrong in my
Windows platform.
you can always use /Za switch to disable microsoft specific
extensions.
Mar 2 '07 #5
On 2 Mar, 10:30, "steve.kim" <precisionma...@gmail.comwrote:

<snip>
I think there is two C++... ANSI C++ and MSVC++.
There are two reasons why a complier might accept non-standard code: a
compiler bug (the compiler author did not mean for it to be accepted)
or a deliberate extension to the language (the compiler author did
mean for it to be accepted). Many implementations of C++ provide
extensions, presumably because the implementor thought they might be
useful to customers. And if you do find an extension useful there is
nothing intrinsically wrong with using it. You just need to bear in
mind that if you try and use that extension on a different
implementation (either when writing a new program or when porting your
existing code), it might not work. Only you know whether that is a
problem for your particular project. And, of course, discussion of
specific extensions belongs in and is most productive in a group
dedicated to the particular extension.

As suggested elsethread, implementations that provide extensions often
also document a means of disabling them.

Gavin Deane

Mar 2 '07 #6
Gavin Deane <de*********@hotmail.comwrote:
On 2 Mar, 08:49, "steve.kim" <precisionma...@gmail.comwrote:
class myClass {
public:
// public data member
struct myStruct { double A, B, C; };
union {
double data[3];
myStruct s;
};
};
>this layout is just for using the data members like below

myClass my;
my.A = SOME_DOUBLE_PRECISION_VALUE;

not using
my.data[0] = ... ; // same my.A

Now my.s.A is the same thing as my.data[0]
It is undefined behavior to assign to one member of a union and then try
to read its value from a different member, but I think there are
exceptions to that rule (common initial sequence PODs or something). I
am not sure if this falls into that category or not, since it might
align the elements of the array differently from the members of the
struct.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Mar 2 '07 #7

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

Similar topics

2
by: d2r2 | last post by:
Hi, I'm trying to run a nested (UNION) query against a MSAccessXP database (version 2002; build 10.6501.6714; SP3) In Access the SQL-statement executes just fine. When I run it in a asp-page I...
2
by: David Lozzi | last post by:
Howdy, I am doing a simple keyword search in my SQL database from my ASP.NET w/ VB application. Currently, I am using a PROC and within the PROC I am doing something like so: SELECT * FROM...
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,
2
by: BerkshireGuy | last post by:
I have the following union query that works great, but I want to alter the query to only return True (-1) for the TEST column. Currently the test column DOES return 0 or -1. I just want the -1....
6
by: ronrsr | last post by:
but I keep getting syntax errors on this one - adict = {'zid': result, 'keywords': result{0], 'quotations':result,'citations':result{3]}; zhtml.print_update_form(adict); result = 22L
1
by: yucikala | last post by:
Hello, I'm a "expert of beginner" in C#. I have a dll - in C. And in this dll is this struct: typedef struct msg_s { /* please make duplicates of strings before next call to emi_read() ! */ ...
2
by: alnoir | last post by:
I've looked around online and have even had a friend help me, however, for some reason I can't compare two strings. I'm doing this at the end of the code (within the two foreach loops), above...
8
by: Gernot Frisch | last post by:
Uhm... Is there any way of making float pos; a union of float x,y,z; somehow, so I can use: mything = mything.y;
1
by: agsupriya86 | last post by:
Hi, I have a LinkedList of arrays ,Each array containing certain keywords..i need to take the union of all keywords presents in all the arrays of the linked list...that is removing the duplicates...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.