[Insight-developers] A few minor consistency issues

Luis Ibanez luis.ibanez at kitware.com
Fri Jan 13 09:31:13 EST 2006


Hi Zach, Kirs,


      Yeap, Kris is right.


The comparison operators (==) and (!=) are not provided
implicitly by the compiler.

Only the copy constructor and the assignment operator are
provided by default. Note that when those are provided
implicitly, they consist of a memory copy of the bytes of
one instance into the bytes of another instance. This may
not be appropriate when classes are supposed to have
unique internal identifiers.


As Kris pointed out, we have to add to all the functors,
methods of the sort:


              bool operator==() const;
              bool operator!=() const;


OR


       Get rid of the check for Functor != arg  in the
       SetFunctor method of the Functor filters.



It is a healthy exercise to add the operator== and !=
to the functors. It is just that, as any other healthy
thing in life, it may seems like a lot of extra work
at the beginning...   until we get use to it.


The way to go, seems to be to look at the Doxygen page
on the Namespace "Functor" (we have tried to put all
the functors in this namespace, but we may have missed
a couple of them...),

http://www.itk.org/Insight/Doxygen/html/namespaces.html

http://www.itk.org/Insight/Doxygen/html/da/de0/namespaceitk_1_1Functor.html

and then from that list, visit all their files and add
the appropriate operators != and ==.

Note that this is only difficult for Functors that have
internal state (e.g. those that have member variables),
which are a minority.  All other Functors that do not have
member variables, can have a standard method operator==
that always returns "true", and an operator != that always
returns "false".

As an example, we just committed a change in the
itkBinaryThresholdImageFilter.h


        Insight/Code/BasicFilters/
            itkBinaryThresholdImageFilter.h


where the first part of the file declares the Functor.

The diffs are here in ViewCVS interface:

http://www.itk.org/cgi-bin/viewcvs.cgi/Code/BasicFilters/itkBinaryThresholdImageFilter.h?root=Insight&sortby=date&r2=1.6&r1=1.5



    Regards,


        Luis



-----------------------
Kris Thielemans wrote:
> 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
> 
> _______________________________________________
> Insight-developers mailing list
> Insight-developers at itk.org
> http://www.itk.org/mailman/listinfo/insight-developers
> 
> 



More information about the Insight-developers mailing list