无法将int赋给返回类的成员int

时间:2011-08-16 05:39:36

标签: c++ smart-pointers

不能100%确定我的问题措辞是否正确,因为我不完全理解我的问题。

对于我的课程,我需要创建自己的智能指针,以便自行清理。

到目前为止,这是我的代码:

部首:

class Test
{
    public:
        Test()
        {
            m_iTest1 = 4;
            m_iTest2 = 3;
            m_iTest3 = 2;
            m_iTest4 = 1;
        }

        Test (int a, int b, int c, int d)
        {
            m_iTest1 = a;
            m_iTest2 = b;
            m_iTest3 = c;
            m_iTest4 = d;
        }

        Test(const Test& a_oTest)
        {
            m_iTest1 = a_oTest.m_iTest1;
            m_iTest2 = a_oTest.m_iTest2;
            m_iTest3 = a_oTest.m_iTest3;
            m_iTest4 = a_oTest.m_iTest4;
        }

        ~Test(){;}

        int m_iTest1;
        int m_iTest2;
        int m_iTest3;
        int m_iTest4;
};

template<class T>
class SmartData
{
    public:
        template<class T> friend class SmartPointer;

        SmartData();
        SmartData(const T& a_oData);
        ~SmartData();

        T operator * () const;
        unsigned int GetCount(){return m_uiCount;}

    protected:
        void IncrementCount(){++m_uiCount;}

        void DecrementCount();

        void DeleteThis();

        unsigned int m_uiCount;
        T* m_poData;
};

template<class T>
class SmartPointer
{
    public:
        SmartPointer();
        SmartPointer(SmartData<T>& a_oSmartData);
        SmartPointer(const SmartPointer& a_oSmartPointer);
        ~SmartPointer();

        SmartPointer<T>& operator = (const SmartPointer<T>& a_oSmartPointer);

        T operator *() const;
        SmartData<T>* operator ->() const;

        unsigned int GetCount() const;      

    private:
        SmartData<T>* m_poSmartData;
};

#include "smartpointer.inl"

内联文件:

template<class T>
SmartData<T>::SmartData()
{
    m_uiCount = 0;
    m_poData = new T();
}

template<class T>
SmartData<T>::SmartData(const T& a_oData)
{
    m_uiCount = 0;
    m_poData = new T(a_oData);
}

template<class T>
SmartData<T>::~SmartData()
{
    if (m_poData)
    {
        delete m_poData;
    }
}

template<class T>
T SmartData<T>::operator * () const
{
    return *m_poData;
}

template<class T>
void SmartData<T>::DecrementCount()
{
    if (m_uiCount - 1 == 0 || m_uiCount == 0)
    {
        DeleteThis();

        return;
    }

    --m_uiCount;    
}

template<class T>
void SmartData<T>::DeleteThis()
{
    if (m_poData)
    {
        delete m_poData;
        m_poData = 0;
    }
}

template<class T>
SmartPointer<T>::SmartPointer()
{
    m_poSmartData = new SmartData<T>();
    m_poSmartData->IncrementCount();
}

template<class T>
SmartPointer<T>::SmartPointer(SmartData<T>& a_oSmartData)
{
    m_poSmartData = &a_oSmartData;
    m_poSmartData->IncrementCount();
}

template<class T>
SmartPointer<T>::SmartPointer(const SmartPointer& a_oSmartPointer)
{
    m_poSmartData = a_oSmartPointer.a_oSmartData;
    m_poSmartData->IncrementCount();
}

template<class T>
SmartPointer<T>::~SmartPointer()
{
    m_poSmartData->DecrementCount();
    m_poSmartData = 0;
}

template<class T>
SmartPointer<T>& SmartPointer<T>::operator = (const SmartPointer<T>& a_oSmartPointer)
{
    m_poSmartData = a_oSmartPointer.m_poSmartData;
    m_poSmartData->IncrementCount();
}

template<class T>
T SmartPointer<T>::operator *() const
{
    return *m_poSmartData->m_poData;
}

template<class T>
SmartData<T>* SmartPointer<T>::operator ->() const
{
    return m_poSmartData;
}

template<class T>
unsigned int SmartPointer<T>::GetCount() const
{
    return m_poSmartData->m_uiCount;
}

的main.cpp

void SomeFunction1(SmartData<Test>& a_SmartData)
{
    SmartPointer<Test> oSmartPointer2(a_SmartData);
}

void main()
{
    SmartData<int> oSmartData1(5);

    if (1)
    {       
        SmartPointer<int> oSmartPointer1(oSmartData1);

        int iTemp1 = oSmartPointer1->GetCount();
        int iTemp2 = *oSmartPointer1;
        int iTemp3 = *oSmartData1;
    }

    if (1)
    {
        SmartData<int> oSmartData2(5);
    }

    SmartData<Test> oSmartData3;

    (*oSmartData3).m_iTest1 = 5; //Does not work

    if (1)
    {
        SmartData<Test> oSmartData4(oSmartData3);

        SomeFunction1(oSmartData3);

        //oSmartData4 still exits
    }
}

一切正常,数据自我清理后我没有泄漏......除了一行:

(*oSmartData3).m_iTest1 = 5;

我正在使用visual studio编译,当我放置“。”时。在“(* oSmartData3)”之后...“m_iTest1”正确显示。除了我收到错误: 错误C2106:'=':左操作数必须是l值

我不确定为什么这不起作用或改变什么,所以它确实有用。

3 个答案:

答案 0 :(得分:4)

仔细观察SmartData中的运算符*()声明:

T operator * () const;

这意味着此运算符返回T类型的对象,该对象是m_poSmartData->m_poData的副本。在这种情况下,它是一个临时对象:

(*oSmartData3).m_iTest1 = 5; //Does not work

当然,您不能为临时对象赋值,因为它不是l值。阅读更多关于l值和r值的信息:http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/index.jsp?topic=%2Fcom.ibm.vacpp7a.doc%2Flanguage%2Fref%2Fclrc05lvalue.htm

我建议您返回对m_poSmartData->m_poData的引用  在operator *()中(如果我正确理解你要做的事情)。

答案 1 :(得分:2)

您的T operator *() const正在返回一个临时对象(即副本),该对象不是l值(无法分配)。改为引用参考:

T& operator *() const;

答案 2 :(得分:0)

这是否有效:

 oSmartData3.m_iTest1 = 5;