Tuesday, October 02, 2007

Effecitve C++ (53-55), and some items in Ed2

Item 53: Pay attention to compiler warnings.
Item 54: Familiarize yourself with the standard library, including TR1
Item.55: Familiarize yourself with Boost.

ED2:
Item 3: Prefer new and delete to malloc and free.

The problem with malloc and free (and their variants) is simple: they don't know about constructors and destructor.
string *stringArray1 = static_cast(malloc(10 * sizeof(string)));
string *stringArray2 = new string[10];

stringArray1 points to enough memory for 10 string objects, but no objects have been constructed in that memory. In contrast, stringArray2 points to an array of 10 fully constructed string objects, each of which can safely be used in any operation taking a string.

Item 4: Prefer C++-style comments
Item 44: Say what you mean; understand what you're saying.

  • A common base class means common traits. If class D1 and class D2 both declare class B as a base, D1 and D2 inherit common data members and/or common member functions from B.
  • Public inheritance means is-a. If class D publicly inherits from class B, every object of type D is also an object of type B, but not vice versa
  • Layering means has-a or is-implemented-in-terms-of. If class A contains a data member of type B, objects of type A either have a component of type B or are implemented in terms of objects of type B
  • A pure virtual function means that only the function's interface is inherited. If a class C declares a pure virtual member function mf, subclasses of C must inherit the interface for mf, and concrete subclasses of C must supply their own implementations for it.
  • A simple virtual function means that the function's interface plus a default implementation is inherited. If a class C declares a simple (not pure) virtual function mf, subclasses of C must inherit the interface for mf, and they may also inherit a default implementation, if they choose.
  • A nonvirtual function means that the function's interface plus a mandatory implementation is inherited. If a class C declares a nonvirtual member function mf, subclasses of C must inherit both the interface for mf and its implementation. In effect, mf defines an invariant over specialization of C.

No comments:

Post a Comment