I am implementing a smartpointer-template and there is one thing that boogles me; how do I increase the reference counter when passing a smartpointer as a parameter to another function? What operator do I overload to increase ref count?
For example:
class test
{
test() { }
~test() { }
};
void A()
{
SmartPointer<test> p;
B(p);
}
void B(SmartPointer<test> p)
{
C(p);
}
void C(SmartPointer<test> p)
{
// p ref count is still 1 and will be destroyed at end of C
}
Thanks
All constructors of your smart pointer must manipulate the refcount, including the copy constructor, and the assignment operator must also be involved.
If this puzzles you, it might be too early for you to write your own smart pointers; instead, you could use the high-quality
std::shared_ptrfor the time being.