I'm trying to create a smart pointer and stumbled over the code below. Since I'm pretty new to C++, its syntax is yet something I have to get used to.
Below you can see the most important part of the code where RC
is the reference counter class with a member variable called count
(of type int
). The function addRef
increments count
with 1.
template < typename T > class SP
{
private:
T* pData; // pointer
RC* reference; // Reference count
public:
SP() : pData(0), reference(0)
{
reference = new RC();
reference->AddRef(); // Increment the reference count
}
//...
}
Now, what exactly happens when the constructor assigns 0
to pData
and reference
?
Further, is there a reason why a "default parameter" was omitted here? Something like this:
SP() : pData(0), reference(new RC())