码迷,mamicode.com
首页 > 其他好文 > 详细

命名的对象是 lvalue

时间:2016-10-13 01:54:34      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

命名的对象是 lvalue

class CText
{
private:
    char *pText;

public:
    void showIt()const
    {
        cout << pText << endl;
    }

    CText(const char* pStr = "No text")
    {
        cout << "CText constructor called" << endl;
        size_t len{ strlen(pStr) + 1 };
        pText = new char[len];
        strcpy_s(pText, len, pStr);
    }

    CText(const CText & txt)
    {
        cout << "CText copy constructor called" << endl;
        size_t len{ strlen(txt.pText) + 1 };
        pText = new char[len];
        strcpy_s(pText, len, txt.pText);
    }

    CText(CText && txt)
    {
        cout << "CText move constructor called" << endl;
        pText = txt.pText;
        txt.pText = nullptr;
    }

    ~CText()
    {
        cout << "CText destructor called" << endl;
        delete[]pText;
    }

    CText & operator=(const CText & txt)
    {
        cout << "CText assignment operator function called" << endl;
        if (this != &txt)
        {
            delete[]pText;
            size_t length{ strlen(txt.pText) + 1 };
            pText = new char[length];
            strcpy_s(pText, length, txt.pText);
        }
        return *this;
    }

    CText & operator=(CText && txt)
    {
        cout << "CText move assignment operator function called" << endl;
        delete[]pText;
        pText = txt.pText;
        txt.pText = nullptr;
        return *this;
    }

    CText operator+(const CText & txt)const
    {
        cout << "CText add operator function called" << endl;
        size_t length{ strlen(pText) + strlen(txt.pText) + 1 };
        CText aText;
        aText.pText = new char[length];
        strcpy_s(aText.pText, length, pText);
        strcat_s(aText.pText, length, txt.pText);
        return aText;
    }
};

GGGG

class CMessage
{
private:
    CText  m_Text;

public:
    void showIt()const
    {
        m_Text.showIt();
    }

    CMessage operator+(const CMessage & aMess) const
    {
        cout << "CMessage add operator function called" << endl;
        CMessage message;
        message.m_Text = m_Text + aMess.m_Text;
        return message;

    }

    CMessage & operator=(const CMessage & aMess)
    {
        cout << "CMessage assignment operator function called" << endl;
        if (this != &aMess)
        {
            m_Text = aMess.m_Text;
        }
        return *this;
    }

    CMessage & operator=(CMessage && aMess)
    {
        cout << "CMessage move assignment operator function called" << endl;
        m_Text = aMess.m_Text;
        return *this;
    }

    CMessage(const char * str = "Default message")//:m_Text{ str }//m_Text { CText(str) }
    {
        cout << "CMessage constructor called----" << endl;
        m_Text = CText(str);
    }

    CMessage(const CMessage & amess)
    {
        cout << "cmessage copy constructor called" << endl;
        m_Text = amess.m_Text;
    }

    CMessage(const CMessage && amess)
    {
        cout << "cmessage move constructor called" << endl;
        m_Text = amess.m_Text;
    }
};

int main()
{
    CMessage motto1{"The devi1 takes care of his own.\n"};

    cout << "----------------------------------------" << endl;

    CMessage motto2{"if yuo sup with the devil use a long spoon.\n"};

    cout << "----------------------------------------" << endl;

    CMessage motto3{motto1+motto2};

    cout << "----------------------------------------" << endl;

    motto3.showIt();
}

 

命名的对象是 lvalue

标签:

原文地址:http://www.cnblogs.com/yunqie/p/5954725.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!