Search code examples
c++pointersconstructordefault-parameters

C++ what happens when 0 is assigned to class variable in constructor


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())

Solution

  • For historical reasons, 0 is equivalent to nullptr, which means "a pointer that does not point to anything". In modern code, you should always use nullptr, never 0.

    Is this the way to go or are there "better" best practices?

    Putting aside concerns about what is being executed, the most idiomatic way to write the same functionality as presented in the question is:

    • Use nullptr to express that a pointer points to nothing.
    • Use member initializers for default member values.
    • Use initializer lists to specify member initial values in place of the default when it differs.
    • Bonus: Always match your news with deletes, even in sample code.
    template < typename T > class SP
    {
    private:
        T*  pData = nullptr;
        RC* reference = nullptr;
    
    public:
        SP() : reference(new RC()) 
        {
            reference->AddRef();
        }
    
        SP(const SP&) = delete;
        SP& operator=(const SP&) = delete;
        
        ~SP() { 
          delete reference; 
        }
    //...
    }