Friday, April 30, 2010

endl

endl flushes cout's stream buffer.

Reference: "Exceptional C++ Style" by Herb Sutter, Addison-Wesley, 2004, p. 7.

Wednesday, April 28, 2010

snprintf()

snprintf() is a standard C function as of C99. Although, it is not a standard C++ function, most compilers add it as an extension.

Reference: "Exceptional C++ Style" by Herb Sutter, Addison-Wesley, 2004

Monday, April 26, 2010

Typedefs

You can define the same typedef multiple times in the same source file, but they must always be the same.

Saturday, April 24, 2010

Arrays and Pointers

The following code is valid:
int *pMyInt = new int[10];
9[pMyInt] = 1;

Because, pMyInt[9] equals *(pMyInt + 9) which equals *(9 + pMyInt) which equals 9[pMyInt].

Thursday, April 22, 2010

Nonvirtual Interface (NVI)

The Nonvirtual Interface (NVI), has two interfaces, a non-virtual interface for the clients, and virtual interfaces for its derived classes.

The author of the following referenced book recommends this idiom over exposing virtual functions to clients.

Reference: "Exceptional C++ Style" by Herb Sutter, Addison-Wesley, 2004, p. 132.

Tuesday, April 20, 2010

Compiler Declared Operators

The compiler implicitly declares the following:
default constructors
copy constructors
copy assignment operators
destructors
They are also implicitly declared inlined.

Reference: "Exceptional C++ Style" by Herb Sutter, Addison-Wesley, 2004, p. 143-145.

Sunday, April 18, 2010

Deques

Deques are not required to be implemented as C-style arrays whereas vectors are.

Reference: "Exceptional C++ Style" by Herb Sutter, Addison-Wesley, 2004, p. 159.

Friday, April 16, 2010

Inline Functions

A compiler can inline functions you did not declare inline (i.e. using 'inline' in the declaration or providing the body in the class declaration).

Reference: "Exceptional C++ Style" by Herb Sutter, Addison-Wesley, 2005, p. 193.

Wednesday, April 14, 2010

Inline Functions

A compiler can refuse to inline a function that you declared inline.

Reference: "Exceptional C++ Style" by Herb Sutter, Addison-Wesley, 2005, p. 193.

Monday, April 12, 2010

Inline Functions

A compiler can inline a function in one place and not in other places.

Reference: "Exceptional C++ Style" by Herb Sutter, Addison-Wesley, 2005, p. 193.

Saturday, April 10, 2010

Source Files Ending in Newline

All C++ source files are required by the C++ standard to end with a newline (and not immediately preceded by a backslash).

Reference: Exceptional C++ Style: 40 New Engineering Puzzles, Programming Problems, and Solutions by Herb Sutter, Addison-Wesley, 2005.,p. 284.

Thursday, April 8, 2010

Vectors

For a vector, v: v.push_back(x) is functionally equivalent to v.insert(v.end(), x).

Reference: "Exceptional C++ Style" by Herb Sutter, Addison-Wesley, 2005., p. 303.

Tuesday, April 6, 2010

Returning a Pointer to Local Variable from a Function

It is bad to return a pointer to a local variable from a function, because the pointer will be dangling when the function returns.

It is ok to return a const reference to a const defined in a function, because the compiler will ensure the lifetime of the const will be at least as long as the lifetime of the const reference.

Sunday, April 4, 2010

Static Variables

For a static class member variable, there is one copy of the variable in the whole program.

If you have member function with a static variable, there is only one copy of the variable in the whole program.

Friday, April 2, 2010

Visitor Design Pattern

In the Visitor Pattern: Visitor implements visit() and Element implements accept().

Reference: Pattern Hatching by John Vlissides, Addison-Wesley, 1998., p. 33.