[ITK-users] Operator overloading in ITK objects.
Bradley Lowekamp
blowekamp at mail.nih.gov
Tue Oct 14 11:38:49 EDT 2014
Hello,
I think the issue is that the variables pair1 and pair2 are SmartPointers not the derived data object type, so your operators are working on a different object type than you expected.
ITK was not designed for use with overloaded operators with it's data objects and process objects.
You could likely hack it by doing (*pair1) == (*pair2), if you really want to use overloaded operators. Otherwise a conventional member function could just as easily be used.
Brad
On Oct 14, 2014, at 11:31 AM, DVigneault <davis.vigneault at gmail.com> wrote:
> All--
>
> I've defined an ITK object inheriting from itk::DataObject (full header file
> copied below). The object has two properties: Index and Value. I've tried
> to overload the == and < operators (basing my syntax off of itk::Point) so
> that the two objects would be equal if they have the same index, and one
> would be less than the other if one's value is less than the other's:
>
> bool operator==(const Self & pt) const {
> return this->GetIndex() == pt->GetIndex(); }
>
> bool operator<(const Self & pt) const {
> return this->GetValue() < pt->GetValue(); }
>
> However, the less than operator isn't functioning as intended:
>
> typedef IndexValuePair< IndexType, PixelType > PairType;
> typename PairType::Pointer pair1 = PairType::New();
> IndexType index1;
> pair1->SetIndex( index1.Fill( 0 ) );
> pair1->SetValue( 1 );
>
> typename PairType::Pointer pair2 = PairType::New();
> IndexType index2;
> pair2->SetIndex( index2.Fill( 1 ) );
> pair2->SetValue( 0 );
>
> std::cout << (pair1 == pair2) << std::endl; // 0
> std::cout << (pair1 == pair1) << std::endl; // 1
> std::cout << (pair1 < pair2) << std::endl; // 1
>
> Interestingly, the results do not change if I comment out the overloading
> operators in the header file, or if I change the values of the pairs,
> suggesting that the program is comparing the objects based on some other
> criteria. Any ideas what I'm doing wrong? Apologies in advance if this is
> more of a c++ question and less of an ITK question--I'm sometimes not sure
> whether my problem is due to my misunderstanding the language or the
> library.
>
> Best,
>
> --Davis
>
> Best, and thanks,
>
> --Davis
>
> *** Header **
>
> #ifndef __itkIndexValuePair_h
> #define __itkIndexValuePair_h
>
> #include "itkDataObject.h"
>
> namespace itk
> {
>
> template < typename IndexType, typename ValueType >
> class IndexValuePair:public DataObject
> {
>
> public:
>
> /** Standard class typedefs. */
> typedef IndexValuePair Self;
> typedef DataObject Superclass;
> typedef SmartPointer< Self > Pointer;
> typedef SmartPointer< const Self > ConstPointer;
>
> /** Method for creation through the object factory. */
> itkNewMacro(Self);
>
> /** Standard part of every itk Object. */
> itkTypeMacro(IndexValuePair, DataObject);
>
> itkSetMacro( Index, IndexType );
> itkGetMacro( Index, IndexType );
>
> itkSetMacro( Value, ValueType );
> itkGetMacro( Value, ValueType );
>
>
> /** Compare two points for equality. */
> bool
> operator==(const Self & pt) const
> {
> return this->GetIndex() == pt->GetIndex();
> }
>
> /** Less than. */
> bool
> operator<(const Self & pt) const
> {
> return this->GetValue() < pt->GetValue();
> }
>
> protected:
> /** Constructor for use by New() method. */
> IndexValuePair() {}
> ~IndexValuePair() {}
> // virtual void PrintSelf(std::ostream & os, Indent indent) const;
>
> IndexType m_Index;
> ValueType m_Value;
>
> private:
> IndexValuePair(const Self &); //purposely not implemented
> void operator=(const Self &); //purposely not implemented
>
> };
>
> }
>
> #endif
>
>
>
> --
> View this message in context: http://itk-insight-users.2283740.n2.nabble.com/Operator-overloading-in-ITK-objects-tp7586346.html
> Sent from the ITK Insight Users mailing list archive at Nabble.com.
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.php
>
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/insight-users
More information about the Insight-users
mailing list