c_-_c_operator_overloading:overloading_the_copy_assignment_operator
C - C++ Operator Overloading - Overloading the copy assignment operator
#include <iostream> #include <cstring> using std::cout; using std::endl; using namespace std; class MyString { public: MyString(const char* pText) { pString = new char[ strlen(pText) + 1 ]; std::strcpy(pString, pText); } ~MyString() { cout << endl << "Destructor called." << endl; delete[] pString; } MyString& operator=(const MyString& String) { cout << endl << "calling =." << endl; if(this == &String) return *this; delete[] pString; pString = new char[ strlen(String.pString) + 1]; // Copy right operand string to left operand std::strcpy(this->pString, String.pString); return *this; } char* getString() const{ return pString; } private: char* pString; }; int main() { MyString warning("There is a String"); MyString standard(""); cout << warning.getString(); cout << standard.getString(); standard = warning; cout << warning.getString(); cout << standard.getString(); cout << endl; return 0; }
c_-_c_operator_overloading/overloading_the_copy_assignment_operator.txt · Last modified: 2020/07/15 09:30 by 127.0.0.1