Hi!
I'm trying to learn operator overloading. But I have some problems grasping the basic idea.
Let's say we have this class:
I probably should implement operator = also to make this work, but this is just to show "+"...Code:class tutClass { char * data; public: tutClass() : data(NULL) {}; tutClass(char *); ~tutClass(); conversion operator + (char *); conversion operator + (tutClass &); } tutClass::tutClass(char * str) { data = new char [ strlen(str) + 1 ]; strcpy(data, str); } ~tutClass() { if (data) delete [] data; } tutClass tutClass::operator + (char * str) { //??? what goes here? //??? something like this? char * temp = new char [ strlen(str) + strlen(data) + 1 ]; strcpy(temp, data); strcat(temp, str); tutClass result(str); delete [] temp; return result; //this isn't right, returning a local var //but how to do it right? } //same for tutClass::operator + (tutClass & tut)
And should it return tutClass, or tutClass &
Any help would be appreciated!
Joren



LinkBack URL
About LinkBacks


