C++ template syntax
This post will cover some less common syntax that is useful when dealing with templates. The class used to demonstrate the syntax is only as an example, it is not supposed to be a good example of a working smart pointer class.
Let's start with a simple SmartPointer class and how to write template function definitions outside of the class definition:
// define the class
//
template<typename PointerType>
class SmartPointer
{
public:
SmartPointer(PointerType *pointer);
~SmartPointer();
private:
PointerType *m_pointer;
};
// define the member functions
//
template<typename PointerType>
inline SmartPointer<PointerType>::SmartPointer(PointerType *pointer)
: m_pointer(pointer)
{
// do some stuff
}
template<typename PointerType>
inline SmartPointer<PointerType>::~SmartPointer(PointerType *pointer)
{
// do some stuff
}