Xref: utzoo comp.object:480 comp.lang.c++:5639 Path: utzoo!attcan!uunet!mcsun!sunic!tut!tukki!sakkinen From: sakkinen@tukki.jyu.fi (Markku Sakkinen) Newsgroups: comp.object,comp.lang.c++ Subject: Re: Dumb question Message-ID: <2122@tukki.jyu.fi> Date: 24 Nov 89 08:39:19 GMT References: <61737@aerospace.AERO.ORG> Reply-To: sakkinen@jytko.jyu.fi (Markku Sakkinen) SAKKINEN@FINJYU.bitnet (alternative) Organization: University of Jyvaskyla, Finland Lines: 52 In article <61737@aerospace.AERO.ORG> abbott@itro3.org (Russell J. Abbott) writes: >As a non-C++ programmer but as someone who has programmed in Smalltalk I >beg your indulgence for a dumb question. The question (that follows) is by no means dumb! It concerns the basic difference between weakly and strongly typed object languages. Although variants of this question have been discussed rather often in these newsgroups, many readers could be interested in a recapitulation. >To what extent can one dynamically bind code in C++? [...] > >More concretely, can one write a sort routine that will sort a list of >any type (perhaps not yet even defined) that responds to "<"? I would >assume that one could, but it wasn't clear to me from the C++ material >that I have how to do it. The answer is No and Yes. No, you cannot write it so that it would apply to _any type_ that responds to "<". Note that, in contrast to more purely OO languages, C++ has many types that are not classes. Yes, with Release 2.0 of C++ now having multiple inheritance, you can replicate the example done in Eiffel in Bertrand Meyer's "Object-oriented Software Construction" (19.4.1, p. 411). You define an abstract class COMPARABLE that declares at least one suitable relational operator as a member function, then use COMPARABLE as one base class of all those classes to which you want your sort routine to apply. But you will have to write the code implementing the member function at least for each immediate subclass ("derived class") of COMPARABLE. This can mean writing "wrapper classes" around existing classes and types, as is done for INTEGER in Meyer (19.4.2, p. 412). Note 1: I have probably read about some (at least proposed) strongly typed OO language(s) in which this can be done more easily, i.e. the sort routine can be defined to apply to all existing classes that have a "<" operation (yielding a Boolean result). Someone name an example? Note 2: An old favourite idea of mine is that classes should offer, as a primitive operation, rather one comparison operator (values: equal, less, greater, incomparable, equivalent; possibly others in special cases) than several relational operators. The latter can be derived from the former, and class interfaces would be simpler. Markku Sakkinen Department of Computer Science University of Jyvaskyla (a's with umlauts) Seminaarinkatu 15 SF-40100 Jyvaskyla (umlauts again) Finland