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... -
-
struct WorldProperty
-
{
-
WORLD_PROP_KEY eKey;
-
-
union value
-
{
-
bool bValue;
-
float fValue;
-
int nValue;
-
};
-
-
bool operator==(const WorldProperty & rhs) const{return eKey == rhs.eKey && value == value;};
-
};
-
-
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?
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
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?
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.
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
by: ccwork |
last post by:
Hi all,
Here is a sample code segment:
....
typedef PACKED struct
{
union
{
PACKED struct
{
char red:1;
|
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)
{...
|
by: SSM |
last post by:
Hi,
Does C standard comment about "Endianness" to be used
to store a structure/union variables?
Thanks & Regards,
Mehta
|
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...
|
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...
|
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
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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...
|
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: 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,...
|
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...
|
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...
|
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,...
| |