operator+, problems...
-
Hey, everyone, I have a problem with overloading operator+, here is the code:
CStr operator+ (const char* val) { CStr temp; char *pBuf=new char[strlen(val)+strlen(buffer)+1]; strcpy(pBuf,buffer); strcat(pBuf,val); temp.buffer=pBuf; return temp; }
Here buffer is a member-variable which holds string data. I don't understand why, but this method never returns concatenated strings, it returns some garbage instead. Any ideas ? -
Hey, everyone, I have a problem with overloading operator+, here is the code:
CStr operator+ (const char* val) { CStr temp; char *pBuf=new char[strlen(val)+strlen(buffer)+1]; strcpy(pBuf,buffer); strcat(pBuf,val); temp.buffer=pBuf; return temp; }
Here buffer is a member-variable which holds string data. I don't understand why, but this method never returns concatenated strings, it returns some garbage instead. Any ideas ? -
Does CStr have a correct copy constructor?
we are here to help each other get through this thing, whatever it is Vonnegut jr.
sighist || Agile Programming | doxygen -
Does CStr have a correct copy constructor?
we are here to help each other get through this thing, whatever it is Vonnegut jr.
sighist || Agile Programming | doxygenHere's my code:
class CStr { private: char *buffer; public: //Construction CStr(void) { buffer=NULL; } CStr(const char *init) { if (init!=NULL) { buffer = new char[strlen(init)+1]; strcpy(buffer,init); } else buffer=NULL; } //Destruction ~CStr(void) { Empty(); } //Operations void Empty() { delete[] buffer; } //Overloads CStr& operator= (const char* val) { Empty(); if (val!=NULL) { buffer = new char[strlen(val)+1]; strcpy(buffer,val); } else buffer=NULL; return *this; } CStr operator+ (const char* val) { CStr temp; char *pBuf=new char[strlen(val)+strlen(buffer)+1]; strcpy(pBuf,buffer); strcat(pBuf,val); temp.buffer=pBuf; return temp; } CStr& operator= (const CStr val) { if (this == &val) return *this; Empty(); buffer = new char[strlen(val.buffer)+1]; strcpy(this->buffer,val.buffer); return *this; } };
I use this class like this:CStr str("Test"); printf("%s",str+"ing");
But, instead of printing Testing it returns lot's of garbage. I ran the debugger, it shows me that operator+ returns bad object :( What shall I do ??? -
Here's my code:
class CStr { private: char *buffer; public: //Construction CStr(void) { buffer=NULL; } CStr(const char *init) { if (init!=NULL) { buffer = new char[strlen(init)+1]; strcpy(buffer,init); } else buffer=NULL; } //Destruction ~CStr(void) { Empty(); } //Operations void Empty() { delete[] buffer; } //Overloads CStr& operator= (const char* val) { Empty(); if (val!=NULL) { buffer = new char[strlen(val)+1]; strcpy(buffer,val); } else buffer=NULL; return *this; } CStr operator+ (const char* val) { CStr temp; char *pBuf=new char[strlen(val)+strlen(buffer)+1]; strcpy(pBuf,buffer); strcat(pBuf,val); temp.buffer=pBuf; return temp; } CStr& operator= (const CStr val) { if (this == &val) return *this; Empty(); buffer = new char[strlen(val.buffer)+1]; strcpy(this->buffer,val.buffer); return *this; } };
I use this class like this:CStr str("Test"); printf("%s",str+"ing");
But, instead of printing Testing it returns lot's of garbage. I ran the debugger, it shows me that operator+ returns bad object :( What shall I do ???So my first idea was right - the class has no corfrect copy CTor ;) OK, here's what happens: when you do
return temp
, the compiler returns a copy oftemp
, and calls the destructor of temp (which deletes temp.buffer). For the copy, C++ looks for a copy constructor with the signatureCStr(CStr const &)
. Since you didn't specify a copy constructor, C++ generates a default one, which makes a "flat" copy (the.buffer
member of the copy is pointing to the same buffer astemp.buffer
, which is deleted in temp's destructor) Provide a correct copy constructoir (similar toCStr(char const *)
, but using aCStr const &
as argument), it will work. --------------------- Some more notes: The behavior of a CStr object passed to printf() (or any other variable argument function) is not defined by the C++ standard. It works on Microsoft Visual C++ (and some MFC code relies on this), but it is not portable. You might want to get a C++ book to learn a bit more about what is necessary to create a "correctly behaving" class, which elements are created by default, etc. Writing a CStr class is an interesting practice, and might be useful if you working on a "no dependency mini exe" Project. However, there are so many reliable string class implementations out there that it rarely makes sense. First there is std::string. Yes, it's ugly, compiler errors (if you do something wrong) are indecipherable, and the VC6 MSDN documentation for it is useless - but it works, it's portable, it's the standard. Second, MFC's "less scientific" but widely successful CString class is available outside of MFC projects as well (on VC7, it's part of the ATL/MFC libraries, on VC6 you can use the semi-official WTL::CString clone, or any other CString clone you find on the internet) good luck!
we are here to help each other get through this thing, whatever it is Vonnegut jr.
sighist || Agile Programming | doxygen -
So my first idea was right - the class has no corfrect copy CTor ;) OK, here's what happens: when you do
return temp
, the compiler returns a copy oftemp
, and calls the destructor of temp (which deletes temp.buffer). For the copy, C++ looks for a copy constructor with the signatureCStr(CStr const &)
. Since you didn't specify a copy constructor, C++ generates a default one, which makes a "flat" copy (the.buffer
member of the copy is pointing to the same buffer astemp.buffer
, which is deleted in temp's destructor) Provide a correct copy constructoir (similar toCStr(char const *)
, but using aCStr const &
as argument), it will work. --------------------- Some more notes: The behavior of a CStr object passed to printf() (or any other variable argument function) is not defined by the C++ standard. It works on Microsoft Visual C++ (and some MFC code relies on this), but it is not portable. You might want to get a C++ book to learn a bit more about what is necessary to create a "correctly behaving" class, which elements are created by default, etc. Writing a CStr class is an interesting practice, and might be useful if you working on a "no dependency mini exe" Project. However, there are so many reliable string class implementations out there that it rarely makes sense. First there is std::string. Yes, it's ugly, compiler errors (if you do something wrong) are indecipherable, and the VC6 MSDN documentation for it is useless - but it works, it's portable, it's the standard. Second, MFC's "less scientific" but widely successful CString class is available outside of MFC projects as well (on VC7, it's part of the ATL/MFC libraries, on VC6 you can use the semi-official WTL::CString clone, or any other CString clone you find on the internet) good luck!
we are here to help each other get through this thing, whatever it is Vonnegut jr.
sighist || Agile Programming | doxygen