[Insight-developers] A few minor consistency issues
Kris Thielemans
kris.thielemans at csc.mrc.ac.uk
Fri Jan 13 04:13:50 EST 2006
Hi Zach
>
> Really? I had always thought that C++ would provide not only
> compiler- generated operator= and copy constructors, but also
> operator== and != which would just compare the memory
> locations for equality.
It even doesn't do != when you defined ==. You have to do that yourself. (in
terms of the other obviously)
>
> I guess this assumption was false. If so, then the only way
> to properly compare two arbitrary instances would be to
> manually do the memory location comparison, right?
>
Not sure what you mean. In general you can't compare 'raw' memory locations.
You don't know the size of the object, its organisation (there might be
holes in how it stores the data). The only way I know is to do a member by
member comparison. This is especially tedious in class hierarchies. You have
to do something like
class Base
{
virtual bool operator==(const Base&) const = 0;
protected:
Bool base_equal(const Base&) const;
};
class Derived: public Base
{
bool operator==(const Derived& x) const
{
if (!base_equal(x)) return false;
// now do rest
}
bool operator==(const Base& x) const
{
Derived const * const ptr = dynamic_cast<Derived> const *>(x);
if (ptr == 0)
return false;
else
return *this == *ptr;
}
}; // end of Derived
And similar stuff if you have intermediate classes in your hierarchy.
Quite a lot of code to write yourself...
You could implement Derived::operator==(const Base& x) const via a mixin
class of course.
Better suggestions anyone?
Kris Thielemans
Hammersmith Imanet, part of GE Healthcare
United Kingdom
More information about the Insight-developers
mailing list