Transitioning from C to C++
The best way to get started with C++ is to start programming in its ANSI C subset. Avoid
the early translators based on cfront, which generated C code rather than machine code.
Using C as a portable machine language really complicated linking and debugging, as
cfront mangled all the function names to encode argument information. Name-mangling is
a horrible kludge which will likely live on in C++ for a long time. Contrast this with Ada,
which does it properly and does not define the semantics by a hacked implementation.
Name-mangling is a hack for doing type checking between different files. But it implies
that all your C++ rules must be complied with the same compiler as the name-mangling
scheme may differ among compilers. This is a big defect in the C++ reuse model as it
effectively prevents reuse at the binary level.
Here is a representative sample of the ways that C is not a C++ subset, to give an idea of
potential trouble spots.
Restrictions in C++ that are not in C:
• The main() routine may not be called by user code in C++. This is permitted (butmost unusual) in C.
• Function prototypes are mandatory in C++, but optional in C.
• Typedef names cannot clash with struct tags in C++, but they can in C (theybelong to separate namespaces).
• A cast is required to assign a void * pointer to another pointer type in C++; nocast is require d in C.
• Features in C++ that mean something different in C:
• There are more than a dozen new keywords in C++. These may be used asidentifiers in a C program, but such use will usually generate an error messagefrom a C++ compiler.
• A declaration can appear anywhere a statement can in C++; in C, declarationsmust appear before statements in a block.
• A struct name in an inner scope will hide an object name in an outer scope in C++,but not in C.
• Character literals have type char in C++, but type int in C. That is,sizeof('a') yields 1 in C++ and a larger value in C.
• Pathological cases involving the // comment convention of C++