5.2.1 · D3C++ Programming

Worked examples — C++ as superset of C — key additions

2,878 words13 min readBack to topic

Before any code: we are comparing two compilers on the same source text. Call them:

  • cc — a C compiler (the C standard, e.g. C11).
  • c++ — a C++ compiler (the C++ standard, e.g. C++17).

"Compiles" means the compiler accepts the text and produces a program. "Error" means it refuses. That is the only measurement we make in most cells below — accept vs reject — plus, where a program runs, what it prints.


The scenario matrix

Every C-to-C++ question this topic can throw falls into one of these cells. Think of it as a checklist of "edge directions" — the corners where C and C++ disagree, plus the corners where they agree.

Cell Axis being tested Concrete trigger
A Pure agreement plain arithmetic C code — compiles identically in both
B Strictness: implicit void* cast int* p = malloc(4); with no cast
C Strictness: reserved word using new, class, bool as an identifier
D Strictness: struct tag as type struct S{}; S x; without repeating struct
E Allocation semantics new/delete vs malloc/free — does a constructor run?
F Reference vs pointer int& alias — degenerate case: can it be null? reseated?
G Templates vs macro MAX macro double-evaluation vs maxv<T>
H Overloading resolution which print overload gets picked; the ambiguous (degenerate) case
I Namespaces two init() colliding; the using shortcut
J Word problem / exam twist a mixed snippet that trips several cells at once

Cells A–D are the "does it even compile?" corners (backward-compatibility limits). E–I are the "it compiles in both, but behaves differently / more safely" corners. J is the combined stress test. The eight worked examples below cover all ten cells.

See the Compilation Model — C vs C++ note for how the same text reaches two different verdicts.

Figure — C++ as superset of C — key additions

Worked examples

Example 1 — Cell A: pure agreement


Example 2 — Cell B: the implicit void* cast

Figure — C++ as superset of C — key additions

Example 3 — Cells C & D: reserved words and struct tags


Example 4 — Cell E: does a constructor run?


Example 5 — Cell F: the reference, and its degenerate limits


Example 6 — Cell G: macro double-evaluation vs template


Example 7 — Cell H: overload resolution, including the ambiguous corner


Example 8 — Cells I & J: namespaces + a combined exam twist


Recall

Recall Cover the answers — reproduce each cell's verdict.
  • Cell B: int* q = malloc(4); — verdict in C vs C++? ::: C ✅, C++ ❌ (no implicit void*int*).
  • Cell C: is int new = 5; legal? ::: C ✅, C++ ❌ (new is a keyword). But int new_ is fine in both.
  • Cell D: struct S{}; S b; (no struct)? ::: C ❌, C++ ✅ (tag is a type in C++).
  • Cell E: malloc-ed Widget, is id guaranteed 42? ::: No — no constructor runs; garbage. new Widget gives 42.
  • Cell F: can an int& be null or reseated? ::: No to both — always bound, always valid.
  • Cell G: macro MAX(i++,j) vs maxv(i++,j) — final i? ::: Macro increments twice (i=7), template once (i=6).
  • Cell H: print(7L) with int/double overloads? ::: Ambiguous → compile error.

Connections

  • ← Back to the parent topic
  • new and delete vs malloc and free
  • Pointers vs References
  • Templates and Generic Programming
  • Namespaces and the std namespace
  • Compilation Model — C vs C++
  • Classes and Objects in C++