December 19, 2000

Forward Declaration of Classes and Structs

You can use the keywords class and struct in forward declarations interchangeably. For example: class X; // fwd declaration uses ‘class’X *p;struct X // definition uses ‘struct'{};void f(){ p=new X;} It works the other way around, too: struct X; X *p;class X {};

Overcoming Debuggers’ Limitations

Many symbolic debuggers limit the length of symbols they can display. For example, the Visual C++ debugger can display strings that contain up to 255 characters. Suppose you have a string that stores a SQL script. Such a script is likely to contain more than 255 characters. However, you cannot

Covariant Template Parameters

Suppose you need to define a function that takes a vector object and performs certain operations on it. The function has to be generic, that is, it should handle all instances of std::vector in the same way. The best way to achieve such generic behavior is by defining the function

Portable Locks

Sometimes you need to make sure that only one instance of the same application can run at any given time. Although every platform defines different locking schemes, you can use the following technique as a platform-independent solution to this problem: at startup, the application should try to open a certain

Function Object Bases

To simplify the process of writing custom function objects, the Standard Library provides two classes that serve as base classes of such objects: std::unary_function and std::binary_function. Both are declared in the header . As the names suggest, unary_function serves as the base of function objects taking one argument and binary_function

The remove_if() algorithm

The remove_if() algorithms (defined in the standard header ) has the following prototype: template ForwardIterator remove (ForwardIterator first, ForwardIterator last, Predicate pred); The first two arguments mark the sequence’s beginning and end. The third argument is a predicate. Unlike remove(), remove_if() uses the predicate provided to select the elements it

The remove() Algorithm

The Standard Library defines the std::remove() algorithm, which moves desired elements to the front of a container and returns an iterator pointing to the end of the sequence of the desired elements. The elements to be removed are positioned past the sequence’s end. remove() is declared in the header as