Overriding operator== Does Not Work in find Algorithm 
Author Message
 Overriding operator== Does Not Work in find Algorithm

I am using a vector template to store pointers to my own class.  I wish to
use the find algorithm to return an item in the vector using our overloaded
== operator.  My class has the following overloaded operator code:

 bool operator==( const MyClass& someClass )
 {
  return (this->value == someClass.value );
 }

Although the find algorithm does an == comparison, my own overloaded ==
function is never called.

Any ideas on what would cause this?

Thomas Scott



Sat, 05 Jun 2004 07:30:47 GMT  
 Overriding operator== Does Not Work in find Algorithm


Quote:
>I am using a vector template to store pointers to my own class.  I wish to
>use the find algorithm to return an item in the vector using our overloaded
>== operator.  My class has the following overloaded operator code:

> bool operator==( const MyClass& someClass )
> {
>  return (this->value == someClass.value );
> }

>Although the find algorithm does an == comparison, my own overloaded ==
>function is never called.

>Any ideas on what would cause this?

find calls operator== on the contained types, in this case pointers.
You either need to use a custom functor that compares MyClass*s, or
you need to use an iterator adaptor like indirect_iterator from
www.boost.org.

Tom



Sat, 05 Jun 2004 19:29:35 GMT  
 Overriding operator== Does Not Work in find Algorithm
Thanks.  I've also discovered that by storing the class itself instead of a
pointer to the class works as well.  If the class is very large, a class
wrapper can be used.

Thomas Scott


Quote:


> >I am using a vector template to store pointers to my own class.  I wish
to
> >use the find algorithm to return an item in the vector using our
overloaded
> >== operator.  My class has the following overloaded operator code:

> > bool operator==( const MyClass& someClass )
> > {
> >  return (this->value == someClass.value );
> > }

> >Although the find algorithm does an == comparison, my own overloaded ==
> >function is never called.

> >Any ideas on what would cause this?

> find calls operator== on the contained types, in this case pointers.
> You either need to use a custom functor that compares MyClass*s, or
> you need to use an iterator adaptor like indirect_iterator from
> www.boost.org.

> Tom



Sun, 06 Jun 2004 01:10:45 GMT  
 Overriding operator== Does Not Work in find Algorithm

Quote:

> I am using a vector template to store pointers to my own class.

If the vector contains pointers, only pointers are searched, and
operator== for pointers is built-in. You can search for known exemplar
pointer or use/define some smart pointer instead and override the == for
it.

hth
Paavo.

Quote:
>  I wish to
> use the find algorithm to return an item in the vector using our overloaded
> == operator.  My class has the following overloaded operator code:

>  bool operator==( const MyClass& someClass )
>  {
>   return (this->value == someClass.value );
>  }

> Although the find algorithm does an == comparison, my own overloaded ==
> function is never called.

> Any ideas on what would cause this?

> Thomas Scott



Mon, 07 Jun 2004 18:50:09 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Not operator NOT working (!)

2. Web Service Not Working -- Namespace not found

3. OnIdle override not working

4. CXX0058: Error: Overloaded Operator Not Found

5. bool operator<(std::string) NOT FOUND

6. IMPORTANT: IntelliSense does not work after __typeof operator

7. update overloading [][] ..working but not with a template operator ..arrrrrrrrrgghghhh

8. Operator+ etc is not working please help!

9. IMPORTANT: IntelliSense does not work after the __typeof operator

10. C++/ATL/ADO - Intellisense not working (statement completion options) not working

11. Find in Files toolbar button in MDS does not work

12. CString::Find not working as advertised

 

 
Powered by phpBB® Forum Software