Andrew Li
Andrew Li

Reputation: 1055

When is C++ Class Constructor called with pointer?

I don't have deep knowledge about c++.
I've implemented the smart pointer to prevent the issues caused by raw pointer. (memory leak)
The code is following for the smart pointer.

#ifndef SMARTPOINTER
#define SMARTPOINTER
class RC
{
    private:
    int count; // Reference count
    public:
    RC(){            count = 0;        }
    void AddRef()
    {
        // Increment the reference count
        count++;
    }
    int Release()
    {
        // Decrement the reference count and
        // return the reference count.
        if(count==0) return 0;
        return --count;
    }
};

template < typename T > class SP
{
private:
    T*    pData;       // pointer
    RC* reference; // Reference count
public:
    SP() : pData(0), reference(0)
    {
        // Create a new reference
        reference = new RC();
        // Increment the reference count
        reference->AddRef();
    }
    SP(T* pValue) : pData(pValue), reference(0)
    {
        // Create a new reference
        reference = new RC();
        // Increment the reference count
        reference->AddRef();
    }
    SP(const SP<T>& sp) : pData(sp.pData), reference(sp.reference)
    {
        // Copy constructor
        // Copy the data and reference pointer
        // and increment the reference count
        reference->AddRef();
    }
    ~SP()
    {
        // Destructor
        // Decrement the reference count
        // if reference become zero delete the data
        if(reference->Release() == 0)
        {
            delete pData;
            delete reference;
        }
    }
    T& operator* ()
    {
        return *pData;
    }
    T* operator-> ()
    {
        return pData;
    }
    SP<T>& operator = (const SP<T>& sp)
    {
        // Assignment operator
        if (this != &sp) // Avoid self assignment
        {
            // Decrement the old reference count
            // if reference become zero delete the old data
            if(reference->Release() == 0)
            {
                delete pData;
                delete reference;
            }

            // Copy the data and reference pointer
            // and increment the reference count
            pData = sp.pData;
            reference = sp.reference;
            reference->AddRef();
        }
        return *this;
    }
};
#endif // SMARTPOINTER

My question is following..
When using this smart pointer class as below

 SP<MyObject> ptrObject;  //MyObject class is the custom class.
 MyObject *ptr = new MyObject;
 ptrObject = ptr;  //This invokes the SP(T *) constructor.. why?
                   //Though operator = ( T*) is not defined.

At this time,though the operator=(T*) function is not defined, why is the constructor of the smart pointer,SP(T* pValue) , called? I thought that the constructor is called when the class object is created.
Please explain this, thanks.

Upvotes: 0

Views: 563

Answers (1)

Talent Developer
Talent Developer

Reputation: 142

Implicit conversions are performed whenever an expression of some type T1 is used in context that does not accept that type, but accepts some other type T2;
in particular:

  • when the expression is used as the argument when calling a function that is declared with T2 as parameter;
  • when the expression is used as an operand with an operator that expects T2;
  • when initializing a new object of type T2, including return statement in a function returning T2;
  • when the expression is used in a switch statement (T2 is integral type);
  • when the expression is used in an if statement or a loop (T2 is bool).

This helps you.
https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.cbclx01/implicit_conversion_sequences.htm

Upvotes: 1

Related Questions