[comp.lang.c++] overloading operators on pointers to a class

ark@alice.UUCP (Andrew Koenig) (05/11/89)

In article <8905091935.AA26577@AENEAS.MIT.EDU>, FISK@BOWDOIN.BITNET writes:

> I would like to overload an operator that is defined on pointers
> to a class.  Here is what I would like to do.  Is this possible?

> struct M { ... };
> typedef struct M* P;
> P a,b,c;
> a = b+c   // the "+" should be something like P::operator+(P,P)

No.

An overloaded operator must have at least one operand that is
a class (not just a pointer to a class).  This is to prevent
you from changing the meaning of built-in operators.

If you really want to do something like that, you can (in C++ 2.0):
define a class called, say Mptr and define Mptr::operator->().
Then you can use an Mptr pretty much as if it were a pointer,
but you can overload functions based on it, give it a constructor
and destructor, and so on.
-- 
				--Andrew Koenig
				  ark@europa.att.com

mat@mole-end.UUCP (Mark A Terribile) (05/11/89)

> I would like to overload an operator that is defined on pointers
> to a class.  Here is what I would like to do.  Is this possible?
 
> struct M { ... };
> typedef struct M* P;
> P a,b,c;
> a = b+c   // the "+" should be something like P::operator+(P,P)


Sorry.  (Gee, I'm beginning to sound like a compiler ...)  What you *can*
do is define a class that can be converted to and from  M*  and upon which
the operations are defined.
-- 

(This man's opinions are his own.)
From mole-end				Mark Terribile