Thread View: net.lang.c++
3 messages
3 total messages
Started by John Rose
Thu, 27 Feb 1986 16:21
the C++ scope operator
Author: John Rose
Date: Thu, 27 Feb 1986 16:21
Date: Thu, 27 Feb 1986 16:21
76 lines
4129 bytes
4129 bytes
(Thanks for the feedback.)
What are some plausible uses of the scope operator "::"?
Here are a few:
(1) Definitions of members outside the main declaration.
(2) Invoking an inherited member function.
(3) Random violations of class abstraction.
Amusingly, the same "::" syntax is used in Common Lisp for something
corresponding to Number 3. Perhaps because of that reinforcement,
something in me winces when I see the scope operator used:
"Get ready for trouble, because someone's poking their nose
where they shouldn't". The scope operator is a danger sign
to readers of code, much like the cast is.
I think Number 1 doesn't set off those alarms because it occurs
in a very specialized context: In a (usually unique) file of
definitions, in top-level declarators.
But (and this is my main point) Number 2, while syntactically
indistinguishable from the alarming Number 3, is a perfectly
safe, reasonable, and common operation. Smalltalk calls it
"sendSuper". Invoking an inherited handler is a necessary
operation in many (most?) abstractions which are specializations.
For example, if you have a class queue with a member put(),
you can derive a class queue_with_census which "wraps" the
generic put() operation with a function which first calls
some element-counting function, and then transfers to the
inherited put().
Expressing sendSuper as if it were an arbitrary violation of
abstraction also has the defect that redundant information
must be specified. The name of the parent (base) class is
available to the compiler, but the programmer must state it
explicitly. This seems unmodular. (Granted, it can't
happen in client code.) Here's an example of how the
unmodularity could hurt: Suppose class der is derived from
class bas, and sendSuper operations are used in der's
implementation. Later, as the hierarchy derived from bas
gets more complicated, the programmer decides to organize
it in two levels. A new "middle" abstraction, class mid,
is interposed between bas and der. Now all uses of the
scope operation in the code for der must be re-examined
to see whether they are intended to mean sendSuper, *and*
whether mid now implements the message. In these cases,
the name "bas" must be replaced with "mid". This is error-prone.
I submit that a more innocuous-looking syntax be conceived for
sendSuper, and the requirement be dropped that the parent class be
named in the construction. Here's a candidate:
der::foo(int m)
{ // pre-actions
return 1+bas::foo(m-1); // Not this,
return 1+continue(m-1); // but this.
}
Two merits: Uses an underused C keyword, and echoes "CONTINUE-WHOPPER"
of ZetaLisp. (Yes, that's not much of an echo; sendSuper is more euphonious.)
Well, put "sendSuper", in whatever form, on the list of future extensions.
Incidentally, it interacts *strongly* with another extension I (and others?)
would like to see eventually: Multiple inheritance. In a single-inheritance
system, it's annoying for the programmer to calculate the name of the
"next class up handling this message"; with multiple inheritance that's
next to impossible. (Besides, a single sendSuper may expand to calls
to *several* mutually unrelated handlers.)
(Multiple inheritance anyone? Anybody thinking about it besides me?
That's a subject for another posting.)
--
----------------------------------------------------------
John R. Rose, Thinking Machines Corporation, Cambridge, MA
245 First St., Cambridge, MA 02142 (617) 876-1111 X270
rose@think.arpa ihnp4!think!rose
Re: the C++ scope operator
Author: keith gorlen
Date: Fri, 28 Feb 1986 17:32
Date: Fri, 28 Feb 1986 17:32
23 lines
1418 bytes
1418 bytes
>must be specified. The name of the parent (base) class is
>available to the compiler, but the programmer must state it
>explicitly. This seems unmodular. (Granted, it can't
I agree. I've been hacking around this by using #define BASE and
#define THIS as identifiers for the base class and this class,
respectively. When I want to refer to a specific class, I use class::
and when I want to refer to the base class I use BASE::.
>(Multiple inheritance anyone? Anybody thinking about it besides me?
>That's a subject for another posting.)
Bye-bye efficiency -- not to mention comprehensibility! It's not
worth it! Maybe in a special dialect called C++Flavors.
--
---
Keith Gorlen
Computer Systems Laboratory
Division of Computer Research and Technology
National Institutes of Health
Bethesda, MD 20892
phone: (301) 496-5363
uucp: {decvax!}seismo!elsie!cecil!keith
Re: the C++ scope operator
Author: Mark Weiser
Date: Sat, 01 Mar 1986 04:01
Date: Sat, 01 Mar 1986 04:01
15 lines
1260 bytes
1260 bytes
In article <54@cecil.UUCP> keith@cecil.UUCP writes:
>
>Bye-bye efficiency -- not to mention comprehensibility! It's not
>worth it! Maybe in a special dialect called C++Flavors.
I don't agree. There should be absolutely no efficiency loss,
and the comprehensibility is much better in some cases because
of better modularity and code reusability. Naturally any new feature
provides a new way to write bad code, but that is not a fair attack.
So did the old features.
-mark
--
Spoken: Mark Weiser ARPA: mark@maryland Phone: +1-301-454-7817
CSNet: mark@umcp-cs UUCP: {seismo,allegra}!umcp-cs!mark
USPS: Computer Science Dept., University of Maryland, College Park, MD 20742
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads