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 overload question

Operator overload question

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
7 Posts 4 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.
  • E Offline
    E Offline
    econy
    wrote on last edited by
    #1

    Please read the following code:

    class MyString {
    private:
    char *str;
    public :
    MyString():str(NULL){}
    MyString(const char* iStr);
    MyString(const MyString& iMyStr);
    ~MyString();

    MyString& operator=(const MyString &iMystr);
    
    //friend MyString& operator+(const char\* iStr, const MyString& iMyStr);
    friend char\* operator+(const char\* iStr, const MyString& iMyStr);
    

    };

    //Friend + , "xxx" + S
    char* operator+(const char* iStr, const MyString& iMyStr)
    {
    char * newStr = NULL;
    newStr = new char[strlen(iStr)+strlen(iMyStr.c_str()) + 1];
    strcpy(newStr,iStr);
    strcat(newStr,iMyStr.c_str());

    return newStr; //works well
        //return MyString(newStr); // compile error
    

    }

    int main()
    {
    MyString S1("fgh");
    MyString S2("abc");

    S1 = "cde" + S1;
    return 0;
    }

    I set break point, and I found if I use commented statements, compiler popups exception. But I think both commented statements ,and non comment statements are same.both use constructor to construct a temporary object. Don't know why it does not work.

    Richard Andrew x64R CPalliniC 2 Replies Last reply
    0
    • E econy

      Please read the following code:

      class MyString {
      private:
      char *str;
      public :
      MyString():str(NULL){}
      MyString(const char* iStr);
      MyString(const MyString& iMyStr);
      ~MyString();

      MyString& operator=(const MyString &iMystr);
      
      //friend MyString& operator+(const char\* iStr, const MyString& iMyStr);
      friend char\* operator+(const char\* iStr, const MyString& iMyStr);
      

      };

      //Friend + , "xxx" + S
      char* operator+(const char* iStr, const MyString& iMyStr)
      {
      char * newStr = NULL;
      newStr = new char[strlen(iStr)+strlen(iMyStr.c_str()) + 1];
      strcpy(newStr,iStr);
      strcat(newStr,iMyStr.c_str());

      return newStr; //works well
          //return MyString(newStr); // compile error
      

      }

      int main()
      {
      MyString S1("fgh");
      MyString S2("abc");

      S1 = "cde" + S1;
      return 0;
      }

      I set break point, and I found if I use commented statements, compiler popups exception. But I think both commented statements ,and non comment statements are same.both use constructor to construct a temporary object. Don't know why it does not work.

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      Well of course it gives a compile error, you have declared the return value of operator+ to be char* instead of MyString&.

      char* operator+(const char* iStr, const MyString& iMyStr)
      {
      char * newStr = NULL;
      newStr = new char[strlen(iStr)+strlen(iMyStr.c_str()) + 1];
      strcpy(newStr,iStr);
      strcat(newStr,iMyStr.c_str());

      return newStr; //works well
          //return MyString(newStr); // compile error ----- DOESN'T MATCH RETURN TYPE
      

      }

      The difficult we do right away... ...the impossible takes slightly longer.

      E 1 Reply Last reply
      0
      • E econy

        Please read the following code:

        class MyString {
        private:
        char *str;
        public :
        MyString():str(NULL){}
        MyString(const char* iStr);
        MyString(const MyString& iMyStr);
        ~MyString();

        MyString& operator=(const MyString &iMystr);
        
        //friend MyString& operator+(const char\* iStr, const MyString& iMyStr);
        friend char\* operator+(const char\* iStr, const MyString& iMyStr);
        

        };

        //Friend + , "xxx" + S
        char* operator+(const char* iStr, const MyString& iMyStr)
        {
        char * newStr = NULL;
        newStr = new char[strlen(iStr)+strlen(iMyStr.c_str()) + 1];
        strcpy(newStr,iStr);
        strcat(newStr,iMyStr.c_str());

        return newStr; //works well
            //return MyString(newStr); // compile error
        

        }

        int main()
        {
        MyString S1("fgh");
        MyString S2("abc");

        S1 = "cde" + S1;
        return 0;
        }

        I set break point, and I found if I use commented statements, compiler popups exception. But I think both commented statements ,and non comment statements are same.both use constructor to construct a temporary object. Don't know why it does not work.

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        Please don't do that. Such a operator should only return a MyString reference.

        Veni, vidi, vici.

        In testa che avete, signor di Ceprano?

        E 1 Reply Last reply
        0
        • Richard Andrew x64R Richard Andrew x64

          Well of course it gives a compile error, you have declared the return value of operator+ to be char* instead of MyString&.

          char* operator+(const char* iStr, const MyString& iMyStr)
          {
          char * newStr = NULL;
          newStr = new char[strlen(iStr)+strlen(iMyStr.c_str()) + 1];
          strcpy(newStr,iStr);
          strcat(newStr,iMyStr.c_str());

          return newStr; //works well
              //return MyString(newStr); // compile error ----- DOESN'T MATCH RETURN TYPE
          

          }

          The difficult we do right away... ...the impossible takes slightly longer.

          E Offline
          E Offline
          econy
          wrote on last edited by
          #4

          In class declaration, it has a constructor:

          MyString(const char * istr);

          L 1 Reply Last reply
          0
          • CPalliniC CPallini

            Please don't do that. Such a operator should only return a MyString reference.

            Veni, vidi, vici.

            E Offline
            E Offline
            econy
            wrote on last edited by
            #5

            but, I use :

            return MyString(newStr);

            I got a compile error message.

            CPalliniC 1 Reply Last reply
            0
            • E econy

              In class declaration, it has a constructor:

              MyString(const char * istr);

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              The implementation of your overload must match the definition, in parameters and return type. You are trying to define the overload as:

              friend MyString& operator+(const char* iStr, const MyString& iMyStr);

              and the implementation as:

              char* operator+(const char* iStr, const MyString& iMyStr)
              {
              }

              The return types are different.

              1 Reply Last reply
              0
              • E econy

                but, I use :

                return MyString(newStr);

                I got a compile error message.

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #7

                You have to change the method signature. It is safe to add a C-like string to a MyString object and then return a reference to the latter:

                MyString & operator+(MyString & iMyStr, const char * str);

                Veni, vidi, vici.

                In testa che avete, signor di Ceprano?

                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