首页 > 代码库 > 命名的对象是 lvalue
命名的对象是 lvalue
命名的对象是 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。