代码之家  ›  专栏  ›  技术社区  ›  Andrew Li

用指针调用C++类构造函数是什么时候?

  •  0
  • Andrew Li  · 技术社区  · 8 年前

    我对c++没有很深的了解。
    我已经实现了智能指针以防止原始指针引起的问题。(内存泄漏)
    下面是智能指针的代码。

    #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
    

    我的问题是。。
    使用此智能指针类时,如下所示

     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.
    

    在这个时候,尽管 operator=(T*) 函数未定义,为什么调用智能指针SP(T*pValue)的构造函数? 我认为在创建类对象时调用构造函数。
    请解释一下,谢谢。

    1 回复  |  直到 8 年前
        1
  •  1
  •   Michael Anderson    8 年前

    每当在上下文中使用某种类型T1的表达式时,如果该表达式不接受该类型,但接受另一种类型T2,则执行隐式转换;
    特别地:

    • 当调用以T2作为参数声明的函数时,将表达式用作参数;
    • 当表达式用作带有期望T2的运算符的操作数时;
    • 初始化T2类型的新对象时,在返回T2的函数中包括return语句;
    • 在switch语句中使用表达式时(T2是整型);
    • 当表达式在if语句或循环中使用时(T2是bool)。

    这对你有帮助。
    https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.cbclx01/implicit_conversion_sequences.htm