Topic: array classes (Novice? question: Designing for multiple inheritance w/ templates)
Author: shang@corp.mot.com (David (Lujun) Shang)
Date: Fri, 17 Jul 92 14:39:28 GMT Raw View
In article <TMB.92Jul16235255@arolla.idiap.ch> tmb@arolla.idiap.ch (Thomas M.
Breuel) writes:
> Inheritance and virtual functions provide polymorphism that is, in
> many ways, much more limited than what you get via templates. The
> difference is that virtual functions are around at runtime, while
> polymorphism-via-templates (usually called "genericity" in the C++
> world) can only take advantage of type information at compile time.
>
I disagree in three points:
1. It is not true that the polymorphism achieved by dynamic binding is much
more limited than the polymorphism achieved by static binding.
2. It is not true that the polymorphism-via-templates can only take advantage
of type information at compile time. Parameterized classes are not necessarily
a static instanciation mechanism. They can be implemented as a dynamic feature
to support dynamic polymorphism (dynamic binding).
3. It is not true that the templates can substitute the inheritance even the
templates can support dynamic binding. As I already memtioned in my previous
poster, inheritance support code reuse through expanding the previous defined
part while parameterized classes support code reuse through specializing the
previous defined part. They cannot be substituted with each other.
David Shang
Author: tmb@arolla.idiap.ch (Thomas M. Breuel)
Date: 19 Jul 92 22:17:07 GMT Raw View
In article <1992Jul17.143928.15719@cadsun.corp.mot.com> shang@corp.mot.com (David (Lujun) Shang) writes:
>In article <TMB.92Jul16235255@arolla.idiap.ch> tmb@arolla.idiap.ch (Thomas M.
>Breuel) writes:
>> Inheritance and virtual functions provide polymorphism that is, in
>> many ways, much more limited than what you get via templates. The
>> difference is that virtual functions are around at runtime, while
>> polymorphism-via-templates (usually called "genericity" in the C++
>> world) can only take advantage of type information at compile time.
>>
>
>I disagree in three points:
>
>1. It is not true that the polymorphism achieved by dynamic binding is much
>more limited than the polymorphism achieved by static binding.
>
>2. It is not true that the polymorphism-via-templates can only take advantage
>of type information at compile time. Parameterized classes are not necessarily
>a static instanciation mechanism. They can be implemented as a dynamic feature
>to support dynamic polymorphism (dynamic binding).
>
>3. It is not true that the templates can substitute the inheritance even the
>templates can support dynamic binding. As I already memtioned in my previous
>poster, inheritance support code reuse through expanding the previous defined
>part while parameterized classes support code reuse through specializing the
>previous defined part. They cannot be substituted with each other.
Maybe we are having problems with terminology. I am sure
that there is some interpretation of the terms
"polymorphism", "inheritance", and "genericity" under which
your disagreement is justified. I suspect that you view
"reuse" mostly as the act of "inheriting" from another
object. I, on the other hand, view "reuse" mostly as the act
of using existing code with newly defined datatypes.
As a matter of practical experience with C++ (and other
languages that support inheritance, polymorphism, runtime
typing, etc.), a few simple facts remain (in response to
your three "disagreements"):
(1) Inheritance only lets you substitute subtypes of some
type "A" for an object that is declared to be of type
"A". Furthermore, using inheritance to achieve
polymorphism does not let you express type constraints
among multiple uses of a type; e.g., you cannot express
the constraint that a return value has to have the same
type as one of the arguments (if you don't understand
what I mean by this, look at the posting you are
replying to; I gave a concrete example). Templates do
not suffer from this restriction.
(2) The C++ template mechanism requires all template
arguments (types or values) to be known at compile time.
(3) I don't think your point (3) is in response to anything
I have written. In any case, in practice, I happen to
find template-style code reuse much more useful than
inheritance-style code reuse; in particular, for array
classes, it is simple and efficient. Apart from this,
while it is true that inheritance only lets you add
members, templates can be used to express arbitrary
derivations of one class from another (i.e., can be used
both to add and remove members), although in C++ you may
have to use a "smart pointer" approach to do this, or
you may have to forward lots of methods by hand.
If you want to take a different perspective, templates
give you capability-based code reuse: any object with
the right capabilities (and the right names for those
capabilities) can be used with a template. In C++,
this is a purely static mechanism, but in a language
like Self, it is dynamic.
Inheritance, on the other hand, only allows you to use
existing functions with objects which inherit from the types
in terms of which the function has been defined (but, you
can mix and match subtypes at liberty at runtime if you use
virtual functions together with inheritance).
Thomas.
Author: shang@corp.mot.com (David (Lujun) Shang)
Date: Mon, 20 Jul 92 15:14:51 GMT Raw View
In article <TMB.92Jul19181707@arolla.idiap.ch> tmb@arolla.idiap.ch (Thomas M.
Breuel) writes:
> I suspect that you view
> "reuse" mostly as the act of "inheriting" from another
> object. I, on the other hand, view "reuse" mostly as the act
> of using existing code with newly defined datatypes.
>
I view "reuse" as a three-dimensional process: extension through inheritance,
specialization through parameterization, combination through structuralization.
No single one can substitute the other two. I have no bias on inheritance, nor
bias on parameterization.
David Shang