April 17, 1998

Use Bit Fields to Store Significant Amounts of Data Optimally

Both C and C++ enable you to store and access data directly in the tiniest possible unit: a bit. This technique is required when dealing with a huge amount of data, for example, on databases containing dozens of millions of records; when memory is scarce (i.e., in embedded systems, hand

When does a virtual member function imposes runtime overhead?

Dynamic binding, for example, having a virtual member(s), is one of the basic principles of OO programming. If the compiler can resolve a call to a virtual member function statically, no extra overhead is incurred and it can even be inlined. In other words, it is as efficient as any

Return type of an overriding virtual member function

Once declared virtual, a member function can be overridden in any level of derivation, as long as the overriding version has the identical signature of the original declaration. This is quite straightforward, but sometimes a virtual has to return an object of type that differs from the return type declared

Use Register variables to enhance performance

The C/C++ register keyword can be used as a hint to the compiler that the declared variable is to be accessed very often during program execution, and hence, should be stored on a machine register instead of RAM, in order to enhance performance. A good example is a loop control

Calling an object’s member function from its constructor

An object’s member functions (both virtual and non-virtual) can be called from its constructor. The invoked virtual is guaranteed to be the one defined in the current object (or higher, if it has not been overridden in the current object). However, virtuals of objects derived from the one whose constructor

Static member function

A static member function in a class can access only other static members of a class or variables which are not members of its class. It can be invoked even without an object instance, unlike any other member functions: class stat{ int num; public: stat(int n = 0) {num=n;} static

Retrieving actual object type in run-time

C++ supports Run-time Type Identification (RTTI), which enables detection of actual object type during program execution using the built-in typeid() operator. Here is an example: //file: classes.hclass base{ base(); virtual ~base();};class derived : public base { derived(); virtual ~derived();};//file: RTTIdemo.cpp#include #include #include “classes.h”using namespace std;void identify(base & b) { //a

Global objects construction and destruction

In object-oriented programming (OOP), global objects are considered harmful. Particularly in C++, there’s even more reason to think twice before using them: a global object construction occurs before a program’s outset, so any exception thrown from its constructor can never be caught. This is also true for a global object’s

When is a mem-initializer required?

A const member or a reference member cannot be initialized within an object’s constructor. Instead, they must be initialized in the constructor’s member-initializer list. Conceptually, mem-initialization takes place before the constructor, so by the time it is invoked, all const and reference members have already been assigned a value: class

No more posts to show