I have written a class in C++ and have defined assignment operator function: cplusplus.com recommends following syntax.
check operator= (const check& obj){*cstr = *(obj.cstr); return *this;}
One another syntax is:
void operator= (const check& obj){*cstr = *(obj.cstr);} # Not returning *this
Would you recommend using first method or the second?
Following is my class:
class check {
private:
string * cstr;
public:
int a;
check (string str) { cstr = new string(str);}
check (const check& obj) {cstr = obj.cstr;}
~check() {cout<<"DES"<<endl; delete cstr; }
check operator= (const check& obj){*cstr = *(obj.cstr); return *this;}
void display () { cout<<*cstr<<endl;}
};
int main () {
check da {"one"};
check pa {"two"};
pa = da;
have (pa);
return 0;
}