Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!seismo!mcvax!ukc!eagle!icdoc!dcw From: dcw@icdoc.UUCP Newsgroups: comp.lang.c++ Subject: Re: Questions About C++ Message-ID: <456@ivax.doc.ic.ac.uk> Date: Fri, 12-Jun-87 12:37:26 EDT Article-I.D.: ivax.456 Posted: Fri Jun 12 12:37:26 1987 Date-Received: Thu, 18-Jun-87 01:00:28 EDT Sender: dcw@doc.ic.ac.uk Reply-To: dcw@doc.ic.ac.uk (Duncan C White) Organization: Dept. of Computing, Imperial College, London, UK. Lines: 56 Summary, part 2/3: Our 'Basic Difference' We thought that SIMULA allows a subclass ---------------------- instance to be assigned to a superclass variable, and that C++ doesn't. Many people pointed out that THIS IS DEAD WRONG : [ ooooppps... ] In SIMULA, the ONLY way of declaring an instance is: ref( fred ) x; // x is an instance of fred, which is a // class declared somewhere above Our mistake was that we assumed that C++, like SIMULA, had ONLY one way to define instances : fred x; // instance of fred whereas, of course, an instance can also be declared using : fred *xptr; // not initialised yet: like SIMULA ref or: fred& xref = x; // must have some initialization with it, // except when its a function parameter So, C++ is quite happy with : vehicle *v = new car; and car c; vehicle &v = c; This, of course, means that you can, after all: >...declare a function which takes a parameter of type vehicle *, >and then call it with an expression which is a car *. > >...define a 'list of vehicles' into which you can put any >subclass-instances [ cars, planes, boats, pogo-sticks, minis, fords, >aston martins etc.. ] Several people commented that C++ would indeed be a weak form of object orientated language if it couldn't handle this kind of thing: this is precisely what we thought [ and had trouble believing ]. NB: Another common problem was mentioned by a few people: if any member of the class hierarchy was not derived publicly from its immediate superclass, even the pointer conversions would not work... For example: class vehicle .. class car : vehicle .. // if 'public' omitted, then vehicle *v = new car; // this declaration is rejected, apparently