Mystring x{"ABC"};
Mystring test="Hi there";
#include<iostream> #include<cstring> class Mystring { private: char * str; public: Mystring(char * s) :str{nullptr} { str = new char[std::strlen(s)+1]; std::strcpy(str,s); std::cout<<"constructor is called for: "<<this->str<<std::endl; } //overaloaded assignment operator Mystring& operator=(const Mystring & rhs) { std::cout<<"copy assignment"<<std::endl; if(this == &rhs) return *this; delete [ ] str; str = new char[std::strlen(rhs.str)+1]; std::strcpy(this->str,rhs.str); return *this; } }; int main() { Mystring x{"ABC"}; Mystring y="Old value"; y="new value"; return 0; }
Mystring
operator=
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)