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 operator() in c++

overloading operator() in c++

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++question
9 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.
  • K Offline
    K Offline
    Kri5
    wrote on last edited by
    #1

    Can anyone help me find what's wrong with operator() overload pls? #include using namespace std; class A { public: int value; A(int a) : value(a) {} operator int() { return value; } void operator ++ () // prefix { value = value+1; } }; int main() { A a1(27); ++a1; // this works fine... // the following does not...MSVC6 tells me "error C2064: term does not evaluate to a function" cout << "value:" << a1() << endl; return 0; } Any ideas?! Many thanks...! Chris

    A M 2 Replies Last reply
    0
    • K Kri5

      Can anyone help me find what's wrong with operator() overload pls? #include using namespace std; class A { public: int value; A(int a) : value(a) {} operator int() { return value; } void operator ++ () // prefix { value = value+1; } }; int main() { A a1(27); ++a1; // this works fine... // the following does not...MSVC6 tells me "error C2064: term does not evaluate to a function" cout << "value:" << a1() << endl; return 0; } Any ideas?! Many thanks...! Chris

      A Offline
      A Offline
      Abhishek Srivastava
      wrote on last edited by
      #2

      yeah there is one idea ... try this cout<<"value:"<< a1.operator int()<< endl ; > in place of ur line cout << "value:" << a1() << endl; :) actually u have overloaded int() operator :) Abhishek Srivastava Software Engineer (VC++) India ,Noida Mobile no 9891492921 :)

      K 1 Reply Last reply
      0
      • A Abhishek Srivastava

        yeah there is one idea ... try this cout<<"value:"<< a1.operator int()<< endl ; > in place of ur line cout << "value:" << a1() << endl; :) actually u have overloaded int() operator :) Abhishek Srivastava Software Engineer (VC++) India ,Noida Mobile no 9891492921 :)

        K Offline
        K Offline
        Kri5
        wrote on last edited by
        #3

        I see...but how do I overload the () operator if i need to use it like this in my code: cout << "value: " << a1() << endl; Chris

        T A 2 Replies Last reply
        0
        • K Kri5

          I see...but how do I overload the () operator if i need to use it like this in my code: cout << "value: " << a1() << endl; Chris

          T Offline
          T Offline
          Taka Muraoka
          wrote on last edited by
          #4

          operator()()


          Lets be honest, isn't it amazing how many truly stupid people you meet during the course of the day. Carry around a pad and pencil, you'll have twenty or thirty names by the end of the day - George Carlin Awasu 1.2 [^]: A free RSS reader with support for Code Project.

          K 1 Reply Last reply
          0
          • K Kri5

            I see...but how do I overload the () operator if i need to use it like this in my code: cout << "value: " << a1() << endl; Chris

            A Offline
            A Offline
            Abhishek Srivastava
            wrote on last edited by
            #5

            for that purpose overload your () operator like this // overloading () operator int operator()() { return value; } now u can access that bracket operator like a1() i hope ur prob is solved now :) regards Abhishek Abhishek Srivastava Software Engineer (VC++) India ,Noida Mobile no 9891492921 :)

            K 1 Reply Last reply
            0
            • A Abhishek Srivastava

              for that purpose overload your () operator like this // overloading () operator int operator()() { return value; } now u can access that bracket operator like a1() i hope ur prob is solved now :) regards Abhishek Abhishek Srivastava Software Engineer (VC++) India ,Noida Mobile no 9891492921 :)

              K Offline
              K Offline
              Kri5
              wrote on last edited by
              #6

              Solved to perfection...many thanks! Chris

              1 Reply Last reply
              0
              • T Taka Muraoka

                operator()()


                Lets be honest, isn't it amazing how many truly stupid people you meet during the course of the day. Carry around a pad and pencil, you'll have twenty or thirty names by the end of the day - George Carlin Awasu 1.2 [^]: A free RSS reader with support for Code Project.

                K Offline
                K Offline
                Kri5
                wrote on last edited by
                #7

                thanks!

                1 Reply Last reply
                0
                • K Kri5

                  Can anyone help me find what's wrong with operator() overload pls? #include using namespace std; class A { public: int value; A(int a) : value(a) {} operator int() { return value; } void operator ++ () // prefix { value = value+1; } }; int main() { A a1(27); ++a1; // this works fine... // the following does not...MSVC6 tells me "error C2064: term does not evaluate to a function" cout << "value:" << a1() << endl; return 0; } Any ideas?! Many thanks...! Chris

                  M Offline
                  M Offline
                  Maxwell Chen
                  wrote on last edited by
                  #8

                  Christopher Spiteri wrote:_

                  class A
                  {
                  public:
                  int value;
                  // ...
                  operator int()
                  {
                  return value;
                  }
                  // ...
                  };

                  _Since there's only one value type operator, int, in your class A, cout will take it implicitly. So you can write it like this:

                  cout << "value:" << a1 << endl;

                  And, operator int() is for something like this:

                  cout << "value:" << (int)a1 << endl;

                  Overloading () would be this way:

                  A& operator ()()
                  {
                  cout << "In operator ()\n";
                  return *this;
                  }

                  Therefore, in main, you can write this:

                  int main()
                  {
                  A a1(27);
                  ++a1; // this works fine...

                  a1(); // This line.
                  return 0;
                  

                  }

                  Maxwell Chen

                  K 1 Reply Last reply
                  0
                  • M Maxwell Chen

                    Christopher Spiteri wrote:_

                    class A
                    {
                    public:
                    int value;
                    // ...
                    operator int()
                    {
                    return value;
                    }
                    // ...
                    };

                    _Since there's only one value type operator, int, in your class A, cout will take it implicitly. So you can write it like this:

                    cout << "value:" << a1 << endl;

                    And, operator int() is for something like this:

                    cout << "value:" << (int)a1 << endl;

                    Overloading () would be this way:

                    A& operator ()()
                    {
                    cout << "In operator ()\n";
                    return *this;
                    }

                    Therefore, in main, you can write this:

                    int main()
                    {
                    A a1(27);
                    ++a1; // this works fine...

                    a1(); // This line.
                    return 0;
                    

                    }

                    Maxwell Chen

                    K Offline
                    K Offline
                    Kri5
                    wrote on last edited by
                    #9

                    I see...this was very clear - thanks a lot mate! Chris

                    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