I had a problem placing an instance of the Reference-counting Pointer <Type>class in an Array class. Using a debugger, it seems that the constructor is never called (which will ruin the reference count and call segfault on the line)!
My push_back function:
void push_back(const T& element)
{
if (length >= max)
reallocate(max > 0 ? max * 2 : 1);
new (&data[length]) T(element);
++length;
}
The reference count is the same as how new is called after. I am very sure that this is a problem, but I cannot understand why the constructor will not be called. Also, the pointer :: Pointer (...) compiles whether the <T>& pointer or the const <T>& pointer (yes?) Is required , and there is a problem no matter how good!
Perhaps there are some details on the placement of new ones, which I do not take into account. If anyone has any thoughts, they will be very grateful!
edit: [ , ]
private:
T* p;
public:
Pointer()
: p(0)
{
}
template<class X> Pointer(Pointer<X>& other)
: p(other.getPointer())
{
if (p)
p->incrementRef();
}
Pointer(T* p)
: p(p)
{
if (p)
p->incrementRef();
}
~Pointer()
{
if (p)
p->decrementRef();
}
= <T> & T*, → T*
quintus