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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Operator Usage

Operator Usage

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
7 Posts 5 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.
  • F Offline
    F Offline
    ForNow
    wrote on last edited by
    #1

    Hi, I was wondering when using a operator such as "+" in order to be invoked does the code being executed have to be part of that class where the operator was defined or with regards for example a "+" operator if one the operands is of a type where the operator has been defined then the opertor code will get invoked. So..... CString + (LPSTR) invoked in any method of any class will execute the CString operator + code thankx

    M _ G 3 Replies Last reply
    0
    • F ForNow

      Hi, I was wondering when using a operator such as "+" in order to be invoked does the code being executed have to be part of that class where the operator was defined or with regards for example a "+" operator if one the operands is of a type where the operator has been defined then the opertor code will get invoked. So..... CString + (LPSTR) invoked in any method of any class will execute the CString operator + code thankx

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      hmmm... not certain I understand correctly, but ... If there's an operator defined for a class it will be called when it is referenced in the code; if the types are not allowable then the compiler will report an error (or warning). In you example, there is an operator that allow the operator+ between a CString and a L(C)PSTR.

      friend CStringT operator+(
      const CStringT& str1,
      PCXSTR psz2
      );

      Watched code never compiles.

      1 Reply Last reply
      0
      • F ForNow

        Hi, I was wondering when using a operator such as "+" in order to be invoked does the code being executed have to be part of that class where the operator was defined or with regards for example a "+" operator if one the operands is of a type where the operator has been defined then the opertor code will get invoked. So..... CString + (LPSTR) invoked in any method of any class will execute the CString operator + code thankx

        _ Offline
        _ Offline
        _Superman_
        wrote on last edited by
        #3

        Normally when you overload an operator in a class, the left has side operand is taken as the caller. So assume you have operator + overloaded in class A. Consider the following code -

        Class A
        {
        public:
        void operator +(int i) {}
        };

        int main()
        {
        A Obj;
        Obj + 3;
        }

        Here the operator call is going to be converted to Obj.operator+(3); But if you write 3 + Obj it will fail because it becomes 3.operator+(Obj) which is illegal in C++. Because of this what is normally done is to create two global functions of the forms -

        void operator +(const A& obj, int i);
        void operator +(int i, const A& obj);

        Then these 2 operators are declared as friend functions of the class A so that the functions can access all members of the class A.

        «_Superman_» I love work. It gives me something to do between weekends.
        Microsoft MVP (Visual C++)

        1 Reply Last reply
        0
        • F ForNow

          Hi, I was wondering when using a operator such as "+" in order to be invoked does the code being executed have to be part of that class where the operator was defined or with regards for example a "+" operator if one the operands is of a type where the operator has been defined then the opertor code will get invoked. So..... CString + (LPSTR) invoked in any method of any class will execute the CString operator + code thankx

          G Offline
          G Offline
          Gwenio
          wrote on last edited by
          #4

          To define an operator out side of a class, you place "friend" before the return type.

          class A {
          public:
          A(int x) :num(x) {}
          ~A() {}
          int GetVal() {return num;}
          friend std::ostream& operator<<(std::ostream& o,A& a) {return o << a.GetVal();}
          private:
          int num;
          };

          Which is the same as:

          class A {
          public:
          A(int x) :num(x) {}
          ~A() {}
          int GetVal() {return num;}
          private:
          int num;
          };
          std::ostream& operator<<(std::ostream& o,A& a) {return o << a.GetVal();}

          modified on Tuesday, April 20, 2010 5:55 PM

          C 1 Reply Last reply
          0
          • G Gwenio

            To define an operator out side of a class, you place "friend" before the return type.

            class A {
            public:
            A(int x) :num(x) {}
            ~A() {}
            int GetVal() {return num;}
            friend std::ostream& operator<<(std::ostream& o,A& a) {return o << a.GetVal();}
            private:
            int num;
            };

            Which is the same as:

            class A {
            public:
            A(int x) :num(x) {}
            ~A() {}
            int GetVal() {return num;}
            private:
            int num;
            };
            std::ostream& operator<<(std::ostream& o,A& a) {return o << a.GetVal();}

            modified on Tuesday, April 20, 2010 5:55 PM

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #5

            Gwenio wrote:

            To define an operator out side of a class, you place "friend" before the return type.

            Only when the 'outside' (i.e. global namespace's) operator (like any other global namespace method) needs to access class protected or private members.

            Gwenio wrote:

            or you might do it like this (have not tried, but is should work): class A { public: A(int x) :num(x) {} ~A() {} int GetVal() {return num;} private: int num; }; friend std::ostream& operator<<(std::ostream& o,A& a) {return o << a.GetVal();}

            Why do you think it works? I doesn't, of course. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            F 1 Reply Last reply
            0
            • C CPallini

              Gwenio wrote:

              To define an operator out side of a class, you place "friend" before the return type.

              Only when the 'outside' (i.e. global namespace's) operator (like any other global namespace method) needs to access class protected or private members.

              Gwenio wrote:

              or you might do it like this (have not tried, but is should work): class A { public: A(int x) :num(x) {} ~A() {} int GetVal() {return num;} private: int num; }; friend std::ostream& operator<<(std::ostream& o,A& a) {return o << a.GetVal();}

              Why do you think it works? I doesn't, of course. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              F Offline
              F Offline
              ForNow
              wrote on last edited by
              #6

              I CString is defined as a friend to most of the MFC class which take strings as arguments

              C 1 Reply Last reply
              0
              • F ForNow

                I CString is defined as a friend to most of the MFC class which take strings as arguments

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

                And so? :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                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