473,796 Members | 2,729 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_PRE CISION_VALUE;

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

Mar 2 '07 #1
6 2584
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_PRE CISION_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*********@ho tmail.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_PRE CISION_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
2169
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 get the following error: Microsoft JET Database Engine (0x80040E10) No value given for one or more required parameters. Can a nested union-query be used at all or should I use an intermediate
2
1568
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 tblStores WHERE strName LIKE '%' + @Search + '%' AND strCity LIKE '%' + @Search + '%' and so on This works GREAT for a single keyword like marshalls. However, if the user
13
5024
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
2270
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. How can I place logic in this without creating another query. Here is the union: SELECT AgencyCode as ContactTypeID, AgencyShortName as ContactName,
6
1240
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
2150
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() ! */ int op_type; /* of "op_t" type: operation type; submit (>0), response (<0) */
2
1833
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 where the 'print "found"' code is located. It works when I use a string rather than the scalar variable '$word'. However, this just isn't feasible in the program that is being developed. If anyone sees anything that I'm doing wrong, I'd really...
8
3714
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
5729
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 words and then combining all.. Can u please prvide me with some help regarding this??
0
10465
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...
1
10200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9061
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
7558
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
6800
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5453
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
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4127
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
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.