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. Overloading the () operator

Overloading the () operator

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
17 Posts 6 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.
  • Y Yadrif

    Hello All, I see some code that looks like this: string ClassA::operator()(int size1, int size2) { ....Some function implementation.... } This appears to me to overload the () operator. Why would someone do this? My understanding of operator overloading is to make code more readable such as with the + and comparison operators. I would think the implementation for this overloaded operator would be called by: ClassA c = new ClassA(); string s = c(10, 20); So, what is the purpose of oerloading the () operator and am I approaching finding where it is called in the code correctly? Thanks for any help. Thanks.

    N Offline
    N Offline
    Nemanja Trifunovic
    wrote on last edited by
    #3

    yadrif wrote:

    So, what is the purpose of oerloading the () operator

    Functors[^], of course.


    Programming Blog utf8-cpp

    Y 1 Reply Last reply
    0
    • M Mark Salsbery

      That's a function call operator. Why?  To make the code less readable, of course :)

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      E Offline
      E Offline
      Emilio Garavaglia
      wrote on last edited by
      #4

      I don't see any need of irony for a perfectly legitimate question, nor I see any need of such an arrogant answer. See the next post [^]

      2 bugs found. > recompile ... 65534 bugs found. :doh:

      M L 2 Replies Last reply
      0
      • E Emilio Garavaglia

        I don't see any need of irony for a perfectly legitimate question, nor I see any need of such an arrogant answer. See the next post [^]

        2 bugs found. > recompile ... 65534 bugs found. :doh:

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #5

        No irony and certainly not anywhere close to the amount of arrogance that seems to be in your post.  Are you the posting police? Actually I was agreeing with the OP. I rarely see use of this that doesn't make the code harder to read than it would be if a meaningful-named method was used. Every time this comes up (function call operator) I'm reminded how lame the example is in the Visual C++ documentation:

        "Given an appropriate overloaded function-call operator, however, this syntax
        can be used to offset the x coordinate 3 units and the y
        coordinate 2 units. The following code shows such a definition:"

        // function_call.cpp
        class Point
        {
        public:
        Point() { _x = _y = 0; }
        Point &operator()( int dx, int dy )
        { _x += dx; _y += dy; return *this; }
        private:
        int _x, _y;
        };

        int main()
        {
        Point pt;
        pt( 3, 2 );
        }

        Cheers, Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        N E H 3 Replies Last reply
        0
        • E Emilio Garavaglia

          I don't see any need of irony for a perfectly legitimate question, nor I see any need of such an arrogant answer. See the next post [^]

          2 bugs found. > recompile ... 65534 bugs found. :doh:

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #6

          emilio_grv wrote:

          I don't see any need

          Thanks, we'll be sure to check with you from now on. Get over yourself :zzz:

          E 1 Reply Last reply
          0
          • M Mark Salsbery

            No irony and certainly not anywhere close to the amount of arrogance that seems to be in your post.  Are you the posting police? Actually I was agreeing with the OP. I rarely see use of this that doesn't make the code harder to read than it would be if a meaningful-named method was used. Every time this comes up (function call operator) I'm reminded how lame the example is in the Visual C++ documentation:

            "Given an appropriate overloaded function-call operator, however, this syntax
            can be used to offset the x coordinate 3 units and the y
            coordinate 2 units. The following code shows such a definition:"

            // function_call.cpp
            class Point
            {
            public:
            Point() { _x = _y = 0; }
            Point &operator()( int dx, int dy )
            { _x += dx; _y += dy; return *this; }
            private:
            int _x, _y;
            };

            int main()
            {
            Point pt;
            pt( 3, 2 );
            }

            Cheers, Mark

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            N Offline
            N Offline
            Nemanja Trifunovic
            wrote on last edited by
            #7

            Mark Salsbery wrote:

            Every time this comes up (function call operator) I'm reminded how lame the example is in the Visual C++ documentation:

            That one is lame, but there are others that are not[^].


            Programming Blog utf8-cpp

            M 1 Reply Last reply
            0
            • N Nemanja Trifunovic

              Mark Salsbery wrote:

              Every time this comes up (function call operator) I'm reminded how lame the example is in the Visual C++ documentation:

              That one is lame, but there are others that are not[^].


              Programming Blog utf8-cpp

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #8

              Indeed. Thanks much for the link (and the wiki link) :)  Seriously. Mark

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              N 1 Reply Last reply
              0
              • L led mike

                emilio_grv wrote:

                I don't see any need

                Thanks, we'll be sure to check with you from now on. Get over yourself :zzz:

                E Offline
                E Offline
                Emilio Garavaglia
                wrote on last edited by
                #9

                :laugh::laugh: Thanks, I appreciate. But, as you can see, that served as a prompt to the author to better explain is position. It's called "reverse psychology" It worked.

                2 bugs found. > recompile ... 65534 bugs found. :doh:

                1 Reply Last reply
                0
                • M Mark Salsbery

                  No irony and certainly not anywhere close to the amount of arrogance that seems to be in your post.  Are you the posting police? Actually I was agreeing with the OP. I rarely see use of this that doesn't make the code harder to read than it would be if a meaningful-named method was used. Every time this comes up (function call operator) I'm reminded how lame the example is in the Visual C++ documentation:

                  "Given an appropriate overloaded function-call operator, however, this syntax
                  can be used to offset the x coordinate 3 units and the y
                  coordinate 2 units. The following code shows such a definition:"

                  // function_call.cpp
                  class Point
                  {
                  public:
                  Point() { _x = _y = 0; }
                  Point &operator()( int dx, int dy )
                  { _x += dx; _y += dy; return *this; }
                  private:
                  int _x, _y;
                  };

                  int main()
                  {
                  Point pt;
                  pt( 3, 2 );
                  }

                  Cheers, Mark

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  E Offline
                  E Offline
                  Emilio Garavaglia
                  wrote on last edited by
                  #10

                  Mark Salsbery wrote:

                  Are you the posting police?

                  Not at all. Just wanted to know the reasons of such a "taliban" position. Now I know. Like many MS MVP you're convinced MS documentations about languages is the Bible. I'm not. I consideri it as just one (of many) sources of information. And Nemania explained what it is for. Now, is your good example of A (better ONE) bad use, enough to say "never use" without any explanation about? I think no, but I couldn't say it, until you didn't provide any motivation about. Of course, fell free to sustain your position, but let every one else to know.

                  2 bugs found. > recompile ... 65534 bugs found. :doh:

                  M 1 Reply Last reply
                  0
                  • M Mark Salsbery

                    Indeed. Thanks much for the link (and the wiki link) :)  Seriously. Mark

                    Mark Salsbery Microsoft MVP - Visual C++ :java:

                    N Offline
                    N Offline
                    Nemanja Trifunovic
                    wrote on last edited by
                    #11

                    Mark Salsbery wrote:

                    Thanks much for the link (and the wiki link) Seriously.

                    You're welcome. In this profession, we all learn all the time, and that's one thing I love about it :)


                    Programming Blog utf8-cpp

                    M 1 Reply Last reply
                    0
                    • N Nemanja Trifunovic

                      Mark Salsbery wrote:

                      Thanks much for the link (and the wiki link) Seriously.

                      You're welcome. In this profession, we all learn all the time, and that's one thing I love about it :)


                      Programming Blog utf8-cpp

                      M Offline
                      M Offline
                      Mark Salsbery
                      wrote on last edited by
                      #12

                      I agree.  Geez, if I didn't learn anything than hanging out at this site would be a complete waste of time :) Mark

                      Mark Salsbery Microsoft MVP - Visual C++ :java:

                      1 Reply Last reply
                      0
                      • E Emilio Garavaglia

                        Mark Salsbery wrote:

                        Are you the posting police?

                        Not at all. Just wanted to know the reasons of such a "taliban" position. Now I know. Like many MS MVP you're convinced MS documentations about languages is the Bible. I'm not. I consideri it as just one (of many) sources of information. And Nemania explained what it is for. Now, is your good example of A (better ONE) bad use, enough to say "never use" without any explanation about? I think no, but I couldn't say it, until you didn't provide any motivation about. Of course, fell free to sustain your position, but let every one else to know.

                        2 bugs found. > recompile ... 65534 bugs found. :doh:

                        M Offline
                        M Offline
                        Mark Salsbery
                        wrote on last edited by
                        #13

                        emilio_grv wrote:

                        Like many MS MVP you're convinced MS documentations about languages is the Bible.

                        Where did you get that from?  You seem to have implied a lot of things from one line of tongue-in-cheek text that wasn't even directed to you. If I didn't post enough information or provide any help to you or the OP then simply ignore my post.  I'm pretty sure all of us who read here do the same. If everyone starts pulling apart useless and bad posts here (and there are LOTS of them, every day), the forums will degenerate pretty quickly. If you knew the slightest thing about me, you'd know that me being an MS MVP is the real irony. I was dragged into using MS dev tools, kicking and screaming.  I had no choice.  I still have no choice. It's market and customer demand.  I do have a choice to not make money but I'm not choosing that yet. Until then, I will make the best of what I have to use, including whatever MS is kind enough to give me. And arrogance?  Give me a break.  I have never claimed to know everything, and have repeatedly stated I know next to nothing.  I'm here on this site to learn and no other reason.  I figure with 4 million+ users potentially able to contribute to a thread, I'm going to pick up some knowledge, and I do.  If that stops, I will move on. Cheers :) Mark

                        Mark Salsbery Microsoft MVP - Visual C++ :java:

                        1 Reply Last reply
                        0
                        • M Mark Salsbery

                          No irony and certainly not anywhere close to the amount of arrogance that seems to be in your post.  Are you the posting police? Actually I was agreeing with the OP. I rarely see use of this that doesn't make the code harder to read than it would be if a meaningful-named method was used. Every time this comes up (function call operator) I'm reminded how lame the example is in the Visual C++ documentation:

                          "Given an appropriate overloaded function-call operator, however, this syntax
                          can be used to offset the x coordinate 3 units and the y
                          coordinate 2 units. The following code shows such a definition:"

                          // function_call.cpp
                          class Point
                          {
                          public:
                          Point() { _x = _y = 0; }
                          Point &operator()( int dx, int dy )
                          { _x += dx; _y += dy; return *this; }
                          private:
                          int _x, _y;
                          };

                          int main()
                          {
                          Point pt;
                          pt( 3, 2 );
                          }

                          Cheers, Mark

                          Mark Salsbery Microsoft MVP - Visual C++ :java:

                          H Offline
                          H Offline
                          Hans Dietrich
                          wrote on last edited by
                          #14

                          I actually use this all the time, in the XTrace.h file I include with some of my projects. I got the idea from this article. Very useful.

                          Best wishes, Hans


                          [CodeProject Forum Guidelines] [How To Ask A Question] [My Articles]

                          M 1 Reply Last reply
                          0
                          • H Hans Dietrich

                            I actually use this all the time, in the XTrace.h file I include with some of my projects. I got the idea from this article. Very useful.

                            Best wishes, Hans


                            [CodeProject Forum Guidelines] [How To Ask A Question] [My Articles]

                            M Offline
                            M Offline
                            Mark Salsbery
                            wrote on last edited by
                            #15

                            I only commented about the original context, because it reminded me of the bad example, and definitely didn't explain myself. Your example has a recognizable context, thus easy for me to read.  Same with some examples in the other posted links. A POINT class with a function operator that takes two ints, and uses those ints to offset the POINT?  Not very clear to me. Thanks Hans, Mark

                            Mark Salsbery Microsoft MVP - Visual C++ :java:

                            H 1 Reply Last reply
                            0
                            • N Nemanja Trifunovic

                              yadrif wrote:

                              So, what is the purpose of oerloading the () operator

                              Functors[^], of course.


                              Programming Blog utf8-cpp

                              Y Offline
                              Y Offline
                              Yadrif
                              wrote on last edited by
                              #16

                              Thanks to everyone for the help. Thanks.

                              1 Reply Last reply
                              0
                              • M Mark Salsbery

                                I only commented about the original context, because it reminded me of the bad example, and definitely didn't explain myself. Your example has a recognizable context, thus easy for me to read.  Same with some examples in the other posted links. A POINT class with a function operator that takes two ints, and uses those ints to offset the POINT?  Not very clear to me. Thanks Hans, Mark

                                Mark Salsbery Microsoft MVP - Visual C++ :java:

                                H Offline
                                H Offline
                                Hans Dietrich
                                wrote on last edited by
                                #17

                                To be completely honest, this TRACE example was the first one that I saw that made me realize how useful it could be. Like you, I had seen "academic" examples, but this one brought it home.

                                Best wishes, Hans


                                [CodeProject Forum Guidelines] [How To Ask A Question] [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