Expert C Programming

(Jeff_L) #1

It doesn't matter. C++ will become widely used in spite of its flaws, and we hope it will eventually
lead the way to something better.


Handy Heuristic

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 (but
    most 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 (they
    belong to separate namespaces).

  • A cast is required to assign a void * pointer to another pointer type in C++; no
    cast 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 as
    identifiers in a C program, but such use will usually generate an error message
    from a C++ compiler.

  • A declaration can appear anywhere a statement can in C++; in C, declarations
    must 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.

Free download pdf