J
JustSomeGuy
I have been trying out the map class in my application and noticed
something I can't explain....
My Key class was
class Key
{
unsigned short group;
unsigned short element;
bool operator< (const Key & r)
{
if (group > r.group) return(false);
if (element >= r.element) return(false);
return true;
}
...
};
I assumed that I also need edcopy constructors and assignment operators
for the Key and Value classes....
So I wrote those as well.....
However when I dump the map list sequentially I noticed that many
key/values were missing...
In fact only entries where both the group and element were unique...
ie.
0x0010, 0x0010 wouldn't exist if 0x0009, 0x0010 existed...
I assumed it was a problem with my operator< for the key class ...
so I re-wrote the key class to combine the two shorts into on long
ie.
unsigned long grp_elem = (unsigned long) group << 16 | (unsigned
long) element;
Then the operator< function looked simply like:
bool operator<(const Key & r)
{
return(grp_elem < r.grp_elem);
}
Then all worked well....
I don't like the fact that I had to do this to my Key class but its the
only way i could get things to work...
Can you see why I wasn't able to get it to work the original way?
TIA
Brian.
something I can't explain....
My Key class was
class Key
{
unsigned short group;
unsigned short element;
bool operator< (const Key & r)
{
if (group > r.group) return(false);
if (element >= r.element) return(false);
return true;
}
...
};
I assumed that I also need edcopy constructors and assignment operators
for the Key and Value classes....
So I wrote those as well.....
However when I dump the map list sequentially I noticed that many
key/values were missing...
In fact only entries where both the group and element were unique...
ie.
0x0010, 0x0010 wouldn't exist if 0x0009, 0x0010 existed...
I assumed it was a problem with my operator< for the key class ...
so I re-wrote the key class to combine the two shorts into on long
ie.
unsigned long grp_elem = (unsigned long) group << 16 | (unsigned
long) element;
Then the operator< function looked simply like:
bool operator<(const Key & r)
{
return(grp_elem < r.grp_elem);
}
Then all worked well....
I don't like the fact that I had to do this to my Key class but its the
only way i could get things to work...
Can you see why I wasn't able to get it to work the original way?
TIA
Brian.