code: #include<iostream> #include<conio.h> #include<cstdio> #include<cstring> using namespace std; class sample{ char *s; public: sample(){s=new char('\0');} sample(const sample &ob); ~sample(){if(s) delete []s; cout<<"Freeing\n";} void show(){cout<<s<<endl;} void set(char *str); sample operator=(sample &ob); }; sample::sample(const sample &ob) { s=new char[strlen(ob.s)+1]; strcpy(s,ob.s); } void sample::set(char *str) { s=new char[strlen(str)+1]; strcpy(s,str); } sample input() { char instr[20]; sample str; cout<<"Enter a string :"; cin>>instr; str.set(instr); return str; } sample sample::operator=(sample &ob) { /* If the target memory is not large enough then allocate new memory. */ if(strlen(ob.s) > strlen(s)) { delete []s; s = new char[strlen(ob.s)+1]; } strcpy(s, ob.s); return *this; } int main() { sample ob; ob=input(); //showing errors ob.show(); _getch(); return 0; }
I have compiled this code in MS Visual C++ Express 2008 and it works as
it should be but when i compile this code in Mingw as a part of GCC ver
4.4.1-2 the input() function should return a temporary object to 'ob'
object and invoke the assignment operator '=' but it doesn't and it
shows me error :"no match for 'operator=' in 'ob=input()()' "
- Assignment Operator Bug Mactavish
- Re: Assignment Operator Bug Miles Bader