473,401 Members | 2,125 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,401 software developers and data experts.

C++ union equality

207 100+
I don't have a C++ compiler with me right now and can't find an answer on the net and I keep on going back and forth in my head on whether this would be legal or not.

So here is my struct in question...

Expand|Select|Wrap|Line Numbers
  1.  
  2. struct WorldProperty
  3. {
  4. WORLD_PROP_KEY eKey;
  5.  
  6.     union value
  7.     {
  8.         bool bValue;
  9.         float fValue;
  10.         int nValue;
  11.     };
  12.  
  13.     bool operator==(const WorldProperty & rhs) const{return eKey == rhs.eKey && value == value;};
  14. };
  15.  
  16.  
So my WORLD_PROP_KEY is an enumeration which tracks which World Property this struct is associated with. My question is will the equality operator work?
Jun 8 '11 #1

✓ answered by Banfa

I tried your code, it doesn't compile (gcc 4.4.0) it gives

bytes.cpp: In member function 'bool WorldProperty::operator==(const WorldProperty&) const':
bytes.cpp:13: error: expected primary-expression before '==' token
bytes.cpp:13: error: expected primary-expression before ';' token

Because value is not an expression, it has no value, and so is not comparable.

4 5405
donbock
2,426 Expert 2GB
I don't know anything about C++, so be skeptical about my comments.

The members of the value union are all different sizes. Suppose you want to compare two structures that both happen to contain bool values. Wouldn't you want the structures to compare as equal if eKey and value.bValue matched, regardless of whether the unused padding in value were different?
Jun 8 '11 #2
hype261
207 100+
Donbock,

That is true, but at run time I won't know what type of information is stored in the union when I do my comparisons. I suppose I could make another enum to track which type got written to, but in that case I probably should make this a class with getters and setters.
Jun 8 '11 #3
Banfa
9,065 Expert Mod 8TB
I tried your code, it doesn't compile (gcc 4.4.0) it gives

bytes.cpp: In member function 'bool WorldProperty::operator==(const WorldProperty&) const':
bytes.cpp:13: error: expected primary-expression before '==' token
bytes.cpp:13: error: expected primary-expression before ';' token

Because value is not an expression, it has no value, and so is not comparable.
Jun 9 '11 #4
hype261
207 100+
Banfa,

Thanks for trying it out. I ended up doing another enum to track which type of data is loaded into the struct so I can do the comparison correctly.
Jun 9 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

40
by: Ike Naar | last post by:
In K&R "The C++ programming language (2nd ANSI C edition), the reference manual states (paragraphs 7.9 and 7.10) that pointer comparison is undefined for pointers that do not point to the same...
56
by: ccwork | last post by:
Hi all, Here is a sample code segment: .... typedef PACKED struct { union { PACKED struct { char red:1;
14
by: Clint Olsen | last post by:
I was wondering if it's considered undefined behavior to use a member of a union when it wasn't initialized with that member. Example: typedef unsigned long hval_t; hval_t hval_init(void) {...
2
by: SSM | last post by:
Hi, Does C standard comment about "Endianness" to be used to store a structure/union variables? Thanks & Regards, Mehta
4
by: Matt Burland | last post by:
I'm a little confused about the way the default equality operator works with classes. Here's the situation, I have two comboboxes that are each filled with different object (i.e. ComboBox1 contains...
37
by: spam.noam | last post by:
Hello, Guido has decided, in python-dev, that in Py3K the id-based order comparisons will be dropped. This means that, for example, "{} < " will raise a TypeError instead of the current...
30
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
2
by: c.a.l | last post by:
Hi, I have found a piece of code which declares union like this: union vector_s { struct {float x,y,z}; float m; } v; there is unnamed structure m ( matrix 1x3 ) which has same offset as x, so...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
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,...
0
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...
0
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...
0
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,...

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.