Tuesday, March 30, 2010

Visitor Design Pattern

The Visitor Pattern has two class hierarchies, Visitor and Element.
Only the Visitor hierarchy can change a lot without causing a maintenance burden.

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

Monday, March 29, 2010

Private Destructor

A class cannot be subclassed if its destructor is private.
Reference: Pattern Hatching by John Vlissides, Addison-Wesley, 1998., p. 40.

Friday, March 26, 2010

Private Members

A very not nice way to get access to private members is to put '#define private public' before the header file #include of the class you want to access the private members of.
Reference: Pattern Hatching by John Vlissides, Addison-Wesley, 1998., p. 47.

Wednesday, March 24, 2010

Abstract Classes

Besides pure virtual functions, another way to make a class abstract is to make the constructors protected.

Reference: Pattern Hatching: Design Patterns Applied by John Vlissides, Addison-Wesley, 1998., p. 113.

Monday, March 22, 2010

References to Arrays

A pointer to an array does not preserve the array bound information, but a reference to an array does.

Reference: "C++ Gotchas" by Stephen C. Dewhurst. Addison-Wesley, 2002., p. 12.

Saturday, March 20, 2010

Conditional Operator

The result of the conditional operator (?:) is an lvalue.
Reference: "C++ Gotchas" by Stephen C. Dewhurst. Addison-Wesley, 2002., p. 15.

Thursday, March 18, 2010

Arrays

parray[-4] could be a valid expression, if parray is of type: 'int *'.
Reference: "C++ Gotchas" by Stephen C. Dewhurst. Addison-Wesley, 2002., p. 16.

Tuesday, March 16, 2010

Switch Statements

The following prints out "One":
#include 
int main() {
switch (1) {
if (0) {
case 1:
cout << "One" << endl;
}
}
}
Reference: "C++ Gotchas" by Stephen C. Dewhurst. Addison-Wesley, 2002., p. 18.

Sunday, March 14, 2010

Function Call Order

In this statement:

i = f() + g();

The order in which the functions are called is not specified.

Reference: "C++ Gotchas" by Stephen C. Dewhurst, Addison-Wesley, 2002., p. 37

Friday, March 12, 2010

Comma

The comma in the following statement is not the comma operator:
for (int i = 0, j = 0; i < 10; ++i) {}It is just part of the declaration statement. Reference: "C++ Gotchas" by Stephen C. Dewhurst, Addison-Wesley, 2002, p. 40.

Wednesday, March 10, 2010

Type Specifiers

The following line compiles:

int const extern i;

Reference: "C++ Gotchas" by Stephen C. Dewhurst. Addison-Wesley, 2002., p. 50.

Monday, March 8, 2010

Constructors

If you want to declare 'name' as a string, you would

do this:
std::string name;
instead of this:
std::string name();
because the second declaration declares a function.

Saturday, March 6, 2010

Constructors

The following two lines are functionally equivalent:
std::string * pName = new std::string();
std::string * pName = new std::string;

Reference: "C++ Gotchas" by Stephen C. Dewhurst, Addison-Wesley, 2002., p. 51.

Thursday, March 4, 2010

Scope

The output from the following code is undefined:int i = 1;{int i = i;cout << i << endl;} Reference: C++ Gotchas by Stephen C. Dewhurst. Addison-Wesley, 2002., p. 53.

Tuesday, March 2, 2010

Scope

The following code prints out a 1:
const int i = 1;
{
enum
{
i = i
};
cout << i << endl; } Note that it looks like it could have undefined behavior, but it doesn't. Reference: "C++ Gotchas" by Stephen Dewhurst Addison-Wesley, 2002., p. 54.