Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. operator+, problems...

operator+, problems...

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sheff
    wrote on last edited by
    #1

    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 ?

    P 1 Reply Last reply
    0
    • S Sheff

      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 ?

      P Offline
      P Offline
      peterchen
      wrote on last edited by
      #2

      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

      S 2 Replies Last reply
      0
      • P peterchen

        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

        S Offline
        S Offline
        Sheff
        wrote on last edited by
        #3

        Well, yes!

        1 Reply Last reply
        0
        • P peterchen

          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

          S Offline
          S Offline
          Sheff
          wrote on last edited by
          #4

          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 ???

          P 1 Reply Last reply
          0
          • S Sheff

            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 ???

            P Offline
            P Offline
            peterchen
            wrote on last edited by
            #5

            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 of temp, and calls the destructor of temp (which deletes temp.buffer). For the copy, C++ looks for a copy constructor with the signature CStr(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 as temp.buffer, which is deleted in temp's destructor) Provide a correct copy constructoir (similar to CStr(char const *), but using a CStr 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

            S 1 Reply Last reply
            0
            • P peterchen

              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 of temp, and calls the destructor of temp (which deletes temp.buffer). For the copy, C++ looks for a copy constructor with the signature CStr(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 as temp.buffer, which is deleted in temp's destructor) Provide a correct copy constructoir (similar to CStr(char const *), but using a CStr 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

              S Offline
              S Offline
              Sheff
              wrote on last edited by
              #6

              Oh, thank's a lot, you helped a lot. I'm writing an interpreter and I needed a specialized string class of my own, and that's the first time I hear about COPY constructor, I programmed a lot, but never heard about it, thanks a lot again :))) ...

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups