[comp.lang.c++] Operator overloading with abstract classes

sorensen@athena.mit.edu (Alma G. Sorensen) (05/17/89)

Howdy, all.  Time again to approach the net with questions.

I'm trying to combine virtual operators with an abstract class.

Basically, I'd like to provide users of the class the ability to
write functions manipulating a family of classes, without knowing
which particular class they were writing operations for.  An
example might be N-dimensional vectors, e.g., I'd like to define a
class base_vec, and then derive classes for vectors of 1, 2, 3,
and 4 dimensions.  Each derived class would have its own set of
overloaded operators.  I figured I could use pointers to the base
class as the access method.  This would allow the users of the
class to write things like the following:

void f(base_vec * a, base_vec * b) {
  base_vec * result;

  result = *a + *b;
}

and have '+' do the appropriate thing for whichever classes happen
to be pointed to by a and b.

I can't do this now because "it is not possible to define an
operator function that operates exclusively on pointers."
[Stroustrup, p 172.]  I can't see the right way to use an abstract
class without pointers, but if I use pointers, I can't do operator
overloading.  Help!  Is there some way to get both?

Thanks,

Greg Sorensen
sorensen@athena.mit.edu

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

In article <11468@bloom-beacon.MIT.EDU>, sorensen@athena.mit.edu (Alma G. Sorensen) writes:

> I can't do this now because "it is not possible to define an
> operator function that operates exclusively on pointers."
> [Stroustrup, p 172.]  I can't see the right way to use an abstract
> class without pointers, but if I use pointers, I can't do operator
> overloading.  Help!  Is there some way to get both?

Define a pair of classes.  One contains the stuff you're
working with, the other contains only a pointer.  This gives
you a class that you can cause to look almost like a pointer,
and you can overload it as you like.
-- 
				--Andrew Koenig
				  ark@europa.att.com