类模板的static成员
下面是c++ primer中模板那一章重新实现的vector的一部分代码:
class Vector {
public:
Vector():elements(0), first_free(0), end(0) {}
void push_back(const T&);
//....
private:
static std::allocator<T> alloc;//object to get raw memory
void reallocate();//get more space and copy existing elements
T* elements;//first element
T* first_free;
T* end;
};
template <typename T> allocator<T> Vector<T>::alloc;
template <typename T>
void Vector<T>::push_back(const T& t) {
if(first_free == end) reallocate();
alloc.construct(first_free, t);
++first_free;
}