15

After reading an amount of C++ articles and tutorials, I often see the phrase "C++ is not C!" or some variant. If this is true, why is C++ trying to be compatible with C? Surely this just encourages C programmers to write bad C++ code? I'm only a newb in the field, so feel free to bombard me with reasons why I'm wrong in thinking that C++ should just make it's mind up and forget C. Ell.

Peter G.
  • 14,786
  • 7
  • 57
  • 75
Ell
  • 4,238
  • 6
  • 34
  • 60
  • You'll get no argument from me. – Benjamin Lindley Mar 05 '11 at 22:24
  • I'm with you in part, in that I personally think there's a lot of silliness in C++. But this question definitely falls into the "subjective and argumentative" category ans will have to be closed. – Fyodor Soikin Mar 05 '11 at 22:26
  • okay... yes it probably will :s – Ell Mar 05 '11 at 22:29
  • right "C++ is not C!" . C is a subset of C++. that's why they are compatible. – Kai Mar 05 '11 at 22:29
  • 7
    @Kai: C is not a subset of C++ [link](http://stackoverflow.com/questions/1201593/c-subset-of-c-where-not-examples/1201840#1201840) – Oleg Svechkarenko Mar 05 '11 at 22:39
  • The real point is that languages are not written and features are not chosen to *prevent* programmers from adopting bad habits. Especially in C++, you're more than allowed to shoot yourself in the foot. The general compatibility with C is orthogonal to the "best practices" one should adopt when writing C++ code. – Cody Gray - on strike Mar 06 '11 at 06:50

4 Answers4

15

The purpose of compatibility with C is so that C++ programs can have convenient access to the billions (trillions?) of lines of existing C code in the world. It was never intended as a suggestion to code C++ as if it was C.

Peripheral objectives were:

  1. Leverage the C skills that many programmers have (given that it is still one of the most widely used languages in the world).
  2. Encourage the use of C++ as a better C, for: a) easing the transition to C++, and b) improve C coding practices for programmers who have no intention of going to C++.
Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • Your main claim is definitely incorrect: source compatibility is neither necessary nor adequate for binary compatibility. There are other programming languages, such as D, that have the same "convenient access" without being source compatible. – Jim Balter Mar 05 '11 at 22:43
  • 1
    @Jim: Source compatibility means that you can include C header files without modification; D requires manual conversion of header files, which is vastly more effort. In any event, whether or not you agree that source compatibility is important (and Stroustrup definitely sides with you on the greater importance of binary compatibility), it was definitely a primary goal in the design of C++, and remains a major consideration when introducing new features. – Marcelo Cantos Mar 05 '11 at 23:07
  • @Jim: P.S.: The "inadequacy" problem is a red herring. Resolving it is as simple as surrounding any declarations that require C linkage with `extern "C" { ... }` (either within the header, or around the `#include`; see [here](http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html#faq-32.2) for a full explanation). This works for C code being called by C++ programs, and C++ code being exported for use by C programs. – Marcelo Cantos Mar 05 '11 at 23:16
  • You said "billions (trillions?) of lines" -- that can't refer to header files. But nice attempt at backtracking. And I didn't say C++ doesn't have a mechanism for compatibility with C code, just that source compatibility isn't generally adequate -- as you bear out, because `extern "C" { ... }` isn't source compatible with C (and I don't need a citation for a "full explanation"; I've been programming since 1965 and was on X3J11 so I know my way around). You could use ifdefs to get compatibility with even completely incompatible sources by conditionally including both sets of source. – Jim Balter Mar 05 '11 at 23:38
9

You can read the historical perspective from the man himself here.

Should C++ forget C? In a sense it already has, the development of the two languages progress independent of each other.

CodeButcher
  • 564
  • 5
  • 16
2

C++ started as "C with classes", and it was just a precompiler that transformed the class & co. syntactic sugar into C code (C was chosen because it was quite widespread, C compiler were available for many platforms); this was one of the reasons it was (is) C-compatible. Another (and maybe more important) one was to remain compatible with the existing C codebase (libraries, syscall, ...), which has been a significant advantage for its widespread usage.

However, during its evolution and standardization, C++ evolved in something quite different.

All the new features that were packed in it (notably advanced OOP capabilities, exceptions, templates) and the evolution of its standard library (especially the inclusion in it of the STL) encouraged new programming styles, that differ significantly from the old "C with classes" style; many common C idioms became obsolete, and had better replacements in C++ (see e.g. std::string vs C-style strings, std::vector vs "normal" heap-allocated arrays). Still, it wasn't a good idea to remove the "older" features, since (1) C compatibility is still important in many cases, (2) the "old" stuff is the foundation for the C++ data structures (std::vector internally uses raw pointers and plain heap arrays) and (3) the background philosophy of C++ is to let the programmer choose.

Since in general the "native" C++ alternatives are better than (usually safer/easier to use/more difficult to misuse, and in general as fast as) the corresponding C idioms, it's usually told to C++ newbies to forget about C and start directly from C++, to avoid picking "C bad habits".

In particular, many C habits (=> e.g. raw pointers) become dangerous when exceptions are used, so it's better that a new programmer starts from the beginning with the idea that his code can be interrupted at any place, and make it exception safe from the beginning wrapping its resources in RAII classes.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • Also, when Bjarne designed C with classes, he belonged to the same department as the C designers. What other language could he have used as a base?! – Bo Persson Mar 06 '11 at 11:09
1

C++ used to be compatible with C (it was even compiled with a C compiler, using preprocessor macros to turn it into C), but then newer versions of C came out, and C++ got it's own compilers, and since then, then languages have become different. Still, with a little care, you can get C code to link properly with C++ code.

Leif Andersen
  • 21,580
  • 20
  • 67
  • 100