[comp.lang.c++] Base class access: by name only?

philip@pescadero.Stanford.EDU (Philip Machanick) (11/19/90)

In some other object-oriented languages, it is possible to access the
superclass (base class in C++) without using its name. In C++, it seems
you have to use the base class name to get to a member redefined in the
current class. This strikes me as inconvenient; if I redesign my class
hierarchy, I have to remember to edit such occurences of base class names
as needed. Am I missing something, or is this a serious limitation?
-- 
Philip Machanick
philip@pescadero.stanford.edu

chip@tct.uucp (Chip Salzenberg) (11/21/90)

According to philip@pescadero.stanford.edu:
>In C++, it seems you have to use the base class name to get to a
>member redefined in the current class.

True, but you can use the preprocessor as a workaround:

   class Base {
       ...
   };

   #define DerivedSuper Base
   class Derived: public Base {
       ...
   };

   Derived::member()
   {
       return DerivedSuper::member() + 1;
   }

-- 
Chip Salzenberg at Teltronics/TCT     <chip@tct.uucp>, <uunet!pdn!tct!chip>
    "I've been cranky ever since my comp.unix.wizards was removed
         by that evil Chip Salzenberg."   -- John F. Haugh II

jimad@microsoft.UUCP (Jim ADCOCK) (11/27/90)

In article <1990Nov19.012937.27283@Neon.Stanford.EDU> philip@pescadero.stanford.edu writes:
|In some other object-oriented languages, it is possible to access the
|superclass (base class in C++) without using its name. In C++, it seems
|you have to use the base class name to get to a member redefined in the
|current class. This strikes me as inconvenient; if I redesign my class
|hierarchy, I have to remember to edit such occurences of base class names
|as needed. Am I missing something, 

Yes, languages that allow accessing a superclass without using its name
only allow single superclasses.  If you have multiple superclasses you
need to be able to specify *which* super class -- the obvious way to do
this is by using the superclasses name.  Which is exactly the convention
C++ chose.

|or is this a serious limitation?

Nor is this a serious limitation.  Many C++ programmers who have chosen
to restrict themselves to using single inheritence [most of these people
presumably being from a Smalltalk background] then choose to define "super"
for use in their derivations:

class Base
{
	// ....
};

#define SUPER base

class derived: public SUPER
{
//	Use the SUPER word
};

Seems to me that if one refactors the class heirachy changing super
classes, hiding the super class name is going to be the *least" of
one's problems!

barmar@think.com (Barry Margolin) (11/30/90)

In article <59306@microsoft.UUCP> jimad@microsoft.UUCP (Jim ADCOCK) writes:
>Yes, languages that allow accessing a superclass without using its name
>only allow single superclasses.  If you have multiple superclasses you
>need to be able to specify *which* super class -- the obvious way to do
>this is by using the superclasses name.

Not necessarily.  Flavors and CLOS define a total ordering of all the
ancestors of a flavor/class; this total ordering is defined to be
consistent with the partial orderings specified by the orders in which the
immediate superclasses are named in the subclass definitions.  When a
method invokes a superclass method the next flavor/class after the current
one in the total order is chosen.  (In CLOS it gets a bit more complicated
because of multimethods, but the same basic idea is used.)

I don't think these systems even provide a way to bypass the ordering and
invoke a specific superclass's method (well, except that the data
structures are visible, so it's possible to do just about anything).
--
Barry Margolin, Thinking Machines Corp.

barmar@think.com
{uunet,harvard}!think!barmar

philip@pescadero.Stanford.EDU (Philip Machanick) (11/30/90)

In article <1990Nov29.175741.116@Think.COM>, barmar@think.com (Barry Margolin) writes:
|> In article <59306@microsoft.UUCP> jimad@microsoft.UUCP (Jim ADCOCK) writes:
|> >Yes, languages that allow accessing a superclass without using its name
|> >only allow single superclasses.  If you have multiple superclasses you
|> >need to be able to specify *which* super class -- the obvious way to do
|> >this is by using the superclasses name.
|> 
|> Not necessarily.  Flavors and CLOS define a total ordering of all the
|> ancestors of a flavor/class; this total ordering is defined to be
|> consistent with the partial orderings specified by the orders in which the
|> immediate superclasses are named in the subclass definitions.  When a
|> method invokes a superclass method the next flavor/class after the current
|> one in the total order is chosen.  (In CLOS it gets a bit more complicated
|> because of multimethods, but the same basic idea is used.)
|> 
|> I don't think these systems even provide a way to bypass the ordering and
|> invoke a specific superclass's method (well, except that the data
|> structures are visible, so it's possible to do just about anything).

In C++, a partial ordering is used, so it would not be possible to allow
an unnamed base class in some cases - just as, in some cases, a named
reference to a base class is ambiguous and therefore an error. This does
not mean such a feature could never be used. Back to my original question:
would it be _useful_? (I was hoping for a response from someone who had
actually used this feature, e.g., in Smalltalk.)
-- 
Philip Machanick
philip@pescadero.stanford.edu

jtt@cunixd.cc.columbia.edu (James T. Tanis) (11/30/90)

I've used the shorthand method available on the Macintosh
(inherited::<method>) and it sure has made my life easier.

In particular, it makes inserting a new class into a _BIG_ heirarchy pretty
much a snap. Also, It would seem not to cause any more ambiguity than the
very existence of multiple inheritance does now.

I most definitely think it should be a candidate for inclusion in ANSI.

-JT