Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!munnari!moncskermit!basser!anucsd!csadfa!cxv From: cxv@csadfa.oz (Christopher Vance) Newsgroups: comp.lang.c++ Subject: Re: dumb question, probably Message-ID: <1097@csadfa.oz> Date: Mon, 10-Aug-87 21:21:48 EDT Article-I.D.: csadfa.1097 Posted: Mon Aug 10 21:21:48 1987 Date-Received: Fri, 14-Aug-87 04:59:19 EDT References: <7164@alice.UUCP> Distribution: comp Organization: Dept. of Computer Science, University College, UNSW, ADFA, Canberra, Australia Lines: 68 in article <7164@alice.UUCP>, ark@alice.UUCP says: > > In article <10120@orchid.waterloo.edu>, gamiddleton@orchid.UUCP writes: [ omitted ] > > A constructor never returns a value, so one does not ever > declare the type of its value. Thus one says > > class foo { > public: > foo(); > }; > > and not > > class foo { > public: > void foo(); > }; `void' (in an ordinary function declaration) means precisely that there is no value returned. Omitting `void' normally means that a function returns an `int'. The intended meaning is unambiguous and correct. Any incorrectness is due: 1) to use of a similar but slightly different syntax for constructors and member functions; and/or 2) to the use of `char' (sometimes? always?) to represent `void'. > Similarly, one just writes > > foo::foo() { } > > and not > > void foo::foo() { } There is an inconsistency, unless one makes the distinction between constructors and ordinary member functions more explicit. A quick look shows that the book does not deal with this point as clearly as it could -- maybe in the fine print. I'll try to suggest a way of explaining the distinction which make the current usage seem more sensible. A destructor has a distinguished declaration syntax, and therefore cannot be confused with an ordinary member function. It definitely returns no value, and so conceptually returns `void', but since it always appears to be invoked implicitly, and never explicitly, we are off the hook. A constructor could be seen as a member function which has a type (its class type), but no function name; or again as a function where the type and name are elided together because they are identical. Unlike a destructor, its declaration and definition CAN be confused with an ordinary member function, and usually has the appearance of being called explicitly. Moreover, it always returns (or constructs) a value by definition, even though the body (of the constructor) doesn't have to say so, since it ends with an implicit `return this'. Perhaps it also needs some distinguishing syntax, rather than just relying on a coincidence of names. Whether it should be seen as returning `void' or the class name type is another matter. It seems to me that defining `void' to mean `char' is just plain wrong if your C compiler already knows that `void' means `no value'. Does C++ use the (ordinary) C compiler defined `void' if it is implemented? Surely most compilers are going this way, as the standardisation process continues. [I haven't looked at this myself because we haven't yet managed to obtain C++; although we have been trying for a while. Apparently we have to deal through AT&T Japan. "Hello? Is anyone there?"] -- Christopher J.S. Vance | cxv@cs.adfa.oz[.au] | gateways: Commun. ACM, Oct. 86