C Language

Working with C++

Why is C's successor called "C plus-plus"? One rationale is that C++ is a "better C" (the first "plus") and adds object-oriented programming (OOP) features (the second "plus"). But C++ brings with these beneficial additions some devilish problems.

Maybe they should have called it "C plus-plus-plus" or "C plus-and-minus." Whatever you call it, C++ requires careful attention to reap its advantages and avoid its problems.

Starting on the Right Foot

Right off the bat, C++ simplifies comments and avoids the danger of the "runaway comments" I described in Chapter 3. If you use // for comments everywhere but in macro definitions, you won't have to worry about where the comment ends - it's always at the end of the same source line. And look how clean comments appear:

 strcpy( title, name ); // Build title

Unfortunately, some C++ compilers' preprocessors may not strip comments from macro definitions, so the following sequence can create problems:

 #define MAX_FILES 10  // Limit to open files
 if ( file_cnt < MAX_FILES ) {

The problem arises if the preprocessor stores the replacement text for MAX_FILES as "10 // Limit to open files". The expanded if statement then becomes

 if (file_cnt < 10 // Limit to open files ) {

which won't compile.

by BrainBellupdated