home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky alt.amateur-comp:428 comp.lang.c:19096 comp.lang.c++:18614 comp.misc:4734
- Path: sparky!uunet!spool.mu.edu!agate!dog.ee.lbl.gov!horse.ee.lbl.gov!torek
- From: torek@horse.ee.lbl.gov (Chris Torek)
- Newsgroups: alt.amateur-comp,comp.lang.c,comp.lang.c++,comp.misc
- Subject: Re: What is Object Oriented Programming? Is C doomed?
- Date: 31 Dec 1992 22:59:48 GMT
- Organization: Lawrence Berkeley Laboratory, Berkeley CA
- Lines: 58
- Message-ID: <28183@dog.ee.lbl.gov>
- References: <1fs9ufINN4ch@usenet.INS.CWRU.Edu> <Bzo97n.DHq@vcd.hp.com>
- NNTP-Posting-Host: 128.3.112.15
-
- In article <Bzo97n.DHq@vcd.hp.com> tonyl@vcd.hp.com (Tony Long) writes:
- > Classic C has no "template" for an abstract structure--the 'C'
- >structures can only contain the data elements themselves and have no direct
- >relationship to functions which operate upon them.
-
- I believe someone else has already pointed this out, but just to be
- absolutely sure, this is true in one sense but false in another. Since
- C has pointer to functions, one can embed such pointers into data
- objects. These correspond quite closely to C++ virtual functions
- (incidentally, all C++ class functions can always be implemented as
- virtual functions, although there may be some cost at runtime to do
- so).
-
- An example will probably make this clear. In C++, one might write
- something like:
-
- class foo {
- int data;
- virtual void function();
- };
-
- (we will ignore the fact that no one would write quite like this :-) ).
- This can be translated directly into C as:
-
- struct foo {
- int data;
- void (*function)(struct foo *);
- };
-
- Of course, given a foo object `object', in C++ one merely writes:
-
- object.function();
-
- where in C one has to write:
-
- object.function(&object);
-
- C also lacks constructors and destructors, which becomes more important
- when dealing with static and automatic objects rather than pointers and
- allocation and free functions; and C++ has finer grained access control
- (private, public, and friends) than does C (all or nothing). In
- effect, C++ automates a number of niggling details that must be written
- out explicitly in C. By automating them, it guarantees that coders
- will not accidentally get them wrong. (Coders can, of course, still
- get the *specifications* wrong, and in C's favor, C++ specifications
- are rather more complex.)
-
- In addition, doing C++-style inheritence in plain C (whether single or
- multiple---both are possible) requires rather extensive use of casts
- and is generally far less convenient and more error-prone. To some
- extent, comparisons between C++ and C are like those between C and
- assembly. I myself am not entirely happy with C++, but I readily
- acknowledge that it is superior to C in a number of ways for a number
- of tasks. (On the other hand, for some tasks assembly is superior to
- both---none of these situations are entirely one-sided.)
- --
- In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 510 486 5427)
- Berkeley, CA Domain: torek@ee.lbl.gov
-