Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site alice.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!alice!bs From: bs@alice.UUCP (Bjarne Stroustrup) Newsgroups: net.lang Subject: C++ Message-ID: <3284@alice.UUCP> Date: Mon, 14-Jan-85 22:10:34 EST Article-I.D.: alice.3284 Posted: Mon Jan 14 22:10:34 1985 Date-Received: Tue, 15-Jan-85 05:42:07 EST Organization: Bell Labs, Murray Hill Lines: 100 Here is a few examples of how C++ can be used to handle some of the ideas/problems mentioned recently on netnews: Comments: C++ has both standard /* */ comments, and // comments to the end of line, each can be used to comment out the other. Union initialization: There is no separate feature for initializing unions in C++. However, the general mechanism for initializing a class object can be used. The two functions called "u" do the job; the correct one is chosen based on the type of the initializer; such functions are called constructors; a constructor has the same name as its class/struct/union: union u { int a; char* p; u(int aa) { a = aa; } u(char* pp) { p = pp; } }; f() { u x = 1; // assignment to x.a u y = "asdf"; // assignment to x.p } The u() functions will be inline substituted, so the code generated in f() reduces to two move instructions: x<-1 and y<-"asdf"; Clearly there could be a more compact notation for specifying this particular initialization, but the full power of a function comes in handy for non-trivial examples. Input and output: Like C, C++ hasn't got any. If you like the printf/scanf family of functions you can use it, or you migth try the stream library. The stream library provides flexible, extendable, and type-secure i/o; output is unformatted. The most common operations are provided in the form of the operator << for output and >> for input. For example, using the standard output stream cout: int i; float d; char* p; ... cout << "i = " << i << " d = " << d << " p = " << p << "\n"; note that each item is written out according to its type. Sometimes this style looks better than printf, sometimes it looks worse, but there can be no type errors and it is easy to define << to take operands of user-defined types. Common part of two structures: First declare the structure that should be common: enum fruit_type { _apple, _pear }; struct fruit { fruit_type type; // more fruit data }; Then use "fruit" as the "base" for "derived" classes: struct apple : public fruit { // apple data }; struct pear : public fruit { // pear data }; f(fruit* p) { switch (p->type) { case _apple: ... case _pear: ... default: cerr << "error, bad fruit\n"; } } External names: C++ cannot be used in any reasonable way on a system that does not support long external names. Some or even most of this may look strange at first glance, but it all fits together, and it all fits with C. More information can be found in the UNIX issue of the BLTJ, and yet more from the AT&T Bell Labs Technical Reports on C++. C++ has been released to educational users ONLY; a prototype C++ compiler is distributed by AT&T Technologies, Inc. Software Sales and Marketing P. O. Box 25000 Greensboro, NC 27420 (or phone 1-800-828-UNIX). - Bjarne Stroustrup, AT&T Bell Labs, Murray Hill, NJ