Eliminating Pointers In Templates

From RAD Studio
Jump to: navigation, search

Go Up to Class Templates Overview Index

Another design technique is to include actual objects, making pointers unnecessary. This can also reduce the number of virtual function calls required, since the compiler knows the actual types of the objects. This is beneficial if the virtual functions are small enough to be effectively inlined. It's difficult to inline virtual functions when called through pointers, because the compiler doesn't know the actual types of the objects being pointed to.

template <class T> aBase {
    .
    .
    .
 private:
  T buffer;
};
class anObject : public aSubject, public aBase<aFilebuf> {
    .
    .
    .
};

All the functions in aBase can call functions defined in aFilebuf directly, without having to go through a pointer. And if any of the functions in aFilebuf can be inlined, you'll get a speed improvement, because templates allow them to be inlined.