In article <eZ*****************@newsread4.news.pas.earthlink. net>,
Oliver Dain <od****@nospam.mindspring.com> wrote:
I've seen it asserted in a few places that C is not a proper subset of
C++, but I don't understand the assertion. What things are valid C
that aren't valid C++?
As an example, this will compile and run (but do nothing interesting)
as C, but not as C++:
--------
/*'.h' form of C headers is bad form in C++ - use <cstdlib> instead*/
#include <stdlib.h>
/*Implicit int - Valid (but deprecated) C, not valid C++*/
main()
{
/*'new' as variable name - Valid C, not valid C++*/
int *new;
/*Implicit conversion from void * - Valid C, not valid C++*/
new=malloc(42*sizeof *new);
/*Implicit conversion *to* void * is valid in both languages*/
free(new);
/*Casting malloc() - bad code in both languages
(Hides bugs like failing to #include <stdlib.h> in C,
new should be used instead of malloc in C++)
*/
/*Character constants with type int - valid C, silent bug in C++
(unless sizeof(int)==sizeof(char))
*/
new=malloc(17*sizeof 'a');
free(new);
/*Falling off the end of main() - different semantics depending on language
C90 returns unspecified exit status, C99 and C++ treat it like 'return 0'
*/
}
--------
Plus there are some more esoteric differences like different handling
of '//' comments that are unlikely to show up in code not deliberately
written to demonstrate them.
Google for 'subset' in comp.lang.c for more than you ever wanted to know
about this.
Note that if you're willing to get a bit more hand-wavey, you can
legitimately claim that the expressive power of C is a subset of the
expressive power of C++ (though this is less true with C99 than with C90);
a C program can be made into a valid C++ program with the same semantics
without any major structural changes (though there will probably be a
bunch of code-level changes that are needed).
dave
--
Dave Vandervies
dj******@csclub.uwaterloo.ca
[Y]ou can write bad code that just barely works in both languages, or good
code that works only in one language -- so pick one, and write good code in
that language. --Chris Torek in comp.lang.c (crossposted to comp.lang.c++)