I have a base class (VectorRaw) and a derived class (Vector).
I use operator new in the constructor of the base class to create a memory buffer and then placement new in the constructor of the derived class to place elements therein.
The base class has its virtual destructor which cleans up if something in the constructor of the derived class goes wrong.
When I try to compile it, there is an error: all of the base class' members (begin, end, end_of_reserved
) are out of scope in all derived classes' functions.
What am I doing wrong?
Here's my code:
template <typename T>
class VectorRaw {
protected:
T * begin;
T * end;
T * end_of_reserved;
VectorRaw(const size_t size) {
begin = (T*) operator new (2 * size * sizeof(T));
end = begin;
end_of_reserved = begin + 2 * size;
}
virtual ~VectorRaw<T> () throw() {
for (T * iter = begin; iter != end; ++iter) {
iter->~T();
}
operator delete (begin);
end_of_reserved = end = begin;
}
};
template <typename T>
class Vector : public VectorRaw<T> {
public:
Vector(const size_t size, const T& value) : VectorRaw<T>(size) {
for (end = begin; end != begin + size; ++end)
{
new (end) T (value);
}
}
bool Empty() const throw() {
return (begin == end);
}
};