Topic: Operator==() question


Author: E.Frejaville@frcl.bull.fr (Etienne Frejaville)
Date: 8 Feb 94 14:49:18 GMT
Raw View
In article <94033.175347PHONKOOP@ESTEC.BITNET>, PHONKOOP@ESTEC.BITNET writes:
|> If I want to overload the operator==() outside a class definition,
|> the compiler I'm using (Borland C++ 3.1) confuses me with an error
|> message:
|>
|> class A {
|>  int a ;
|>  public:
|>   A() { a = 10 ; }
|>   int val()   { return a ; }
|>  } ;
|>
|> int operator==( A& x, int v )  { return x.val() == v ; }
|> int operator==( A* x, int v )  { return x->val() == v ; }
|> Error tst.cpp 16: 'operator ==(A *,int)' must be a member function or have a par
|> ameter of class type
|>
|> Is there a reason why I can overload operator== with a class reference,
|> but not with a class pointer?
|>
|> On the logical level, I can't see any differency.
|>
|> Please comment.
|>
|> Thank you for your effort.
|>
|>
|> Piet Honkoop
|> ====================================================================

ANSI C++ says 13.4 :

"An operator function must either be a non-static member function or take at least one argument of a
class or a reference to a class"

Thus a pointer to a class is not a legal argument for an overloaded operator.

--------Etienne FREJAVILLE---Bull S.A   -------------------------------------
 BSP/OSO/MDW/LPS/TOPAS                  e-mail: E.Frejaville@frcl.bull.fr
 Rue Jean-Jaures, F6/1D/17, BP 53       tel: (33-1) 30806548
 78340 Les Clayes-sous-Bois, France     Fax: (33-1) 30807950








Author: <PHONKOOP@ESTEC.BITNET>
Date: Wed, 2 Feb 1994 17:53:47 CET
Raw View
If I want to overload the operator==() outside a class definition,
the compiler I'm using (Borland C++ 3.1) confuses me with an error
message:

class A {
 int a ;
 public:
  A() { a = 10 ; }
  int val()   { return a ; }
 } ;

int operator==( A& x, int v )  { return x.val() == v ; }
int operator==( A* x, int v )  { return x->val() == v ; }
Error tst.cpp 16: 'operator ==(A *,int)' must be a member function or have a par
ameter of class type

Is there a reason why I can overload operator== with a class reference,
but not with a class pointer?

On the logical level, I can't see any differency.

Please comment.

Thank you for your effort.


Piet Honkoop
====================================================================





Author: pkt@lpi.liant.com (Scott Turner)
Date: Thu, 3 Feb 1994 16:09:20 GMT
Raw View
In article <94033.175347PHONKOOP@ESTEC.BITNET>, <PHONKOOP@ESTEC.BITNET>
wrote:

> Is there a reason why I can overload operator== with a class reference,
> but not with a class pointer?

operator== already has a meaning which applies to pointers.
There's a rule against overloading == for pointers, which is there
to prevent confusion with the built-in meaning of ==.

In your example you had

> int operator==( A* x, int v )  { return x->val() == v ; }

Consider what might happen:

  A *p;
  if (p == 0) { ...   // Does this test for null p?
                      // or does it call user-defined operator==?
--
Scott Turner
Liant Software Corp.
959 Concord St., Framingham, MA 01701  USA
(508)872-8700
pkt@lpi.liant.com




Author: grumpy@cbnewse.cb.att.com (Paul J Lucas)
Date: Thu, 3 Feb 1994 18:02:48 GMT
Raw View