Overloaded Unary Operator ++

This is a discussion on Overloaded Unary Operator ++ within the C++ Programming forums, part of the General Programming Boards category; I have a question that I can't seem to find an answer for, and I am wondering if someone here ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    3

    Overloaded Unary Operator ++

    I have a question that I can't seem to find an answer for, and I am wondering if someone here might be able to lend me some knowledge.

    I am curious as to how the computer knows which member function to call, post or pre-increment, when operator notation is used in the main program. I can understand how it would be know using function notation, such as the following:

    obj1.operator++(); // Pre-increment call
    obj1.operator++(0); // Post-increment call

    But how does it know which one to call if this was the code:

    ++obj1; // Pre-increment call
    obj1++; // Post-increment call

    Please reference the following test code:

    Code:
    class Test
    {
         public:
              Test(signed short int = 0);
              void operator++();
              void operator++(int);
         private:
              signed short int var;
    };
    
    Test::Test(signed short int tvar)
    {
         var = tvar;
    }
    
    void Test::operator++()
    {
         ++var;
    }
    
    void Test::operator++(int x)
    {
         var++;
    }
    
    signed int main()
    {
         Test obj(1);
         ++obj;
         obj++;
         obj.operator++();
         obj.operator++(0);
    }
    Thanks,

    Jaymes76

    Edit: Fixed object name mismatch - didn't change the point of the post, however - just nitpicky on my part.
    Last edited by Jaymes76; 07-24-2003 at 09:07 PM.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    I'm not sure exactly what you are asking. I think you are asking how, at run-time, the determination of which method to call is made? Like whether to use pre-fix or post-fix increment?

    Well when you do something like obj++ it will look for the method with the "dummy" integer as the parameter. If it is found it will use that one.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    3
    MrWizard:

    What you stated more concisely than I did, is in fact what I was asking. I understand what you have replied with; however, lets say for instance for the methods were reversed, i.e.:

    Code:
    void Test::operator++()
    {
         var++;
    }
    
    void Test::operator++(int x)
    {
         ++var;
    }
    From what you have stated, obj++ would then -actually- execute ++obj, correct?

    See - I am having trouble understanding how exactly the determination is made without specifying any dummy parameter in the call, like you would in function notiation ... maybe I am missing something here ...

    Jaymes76

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    The determination isn't made at run-time. The compiler simply generates it during compilation. Nothing mysterious there. It's the same mechanism that's used with normal data types.

  5. #5
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    If you have the following code:

    MyClass obj;
    ++obj;

    this looks for functions of one of the following signatures:

    {any return type} MyClass::operator++();
    {any return type} operator++(const MyClass & );
    {any return type} operator++(MyClass & );
    {any return type} operator++(MyClass );
    etc.

    If you execute obj++;, then it looks for one of the following signatures:

    {any return type} MyClass::operator++(int);
    {any return type} operator++(const MyClass &, int );
    {any return type} operator++(MyClass &, int );
    {any return type} operator++(MyClass, int );

    the second parameter of this ++ operator is unusable. It is a dummy parameter.

    Preincrement operators are declared using traditional unary syntax. Postincrement operators are declared using traditional BINARY syntax. BOTH operators actually function as unary operators.
    Last edited by Cat; 07-24-2003 at 10:25 PM.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by Sebastiani
    The determination isn't made at run-time. The compiler simply generates it during compilation. Nothing mysterious there. It's the same mechanism that's used with normal data types.
    Er, duh. I can't believe I wrote run-time there.

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    3
    Cat,

    Thanks a lot for the information and taking the time to respond. Where I was confused is that I didn't know ++obj always was tied to the function without a dummy and that the obj++ was always tied to the function with a dummy by default. I appreciate your clarifying that for me.

    Thanks go out to MrWizard as well for his input.

    Jaymes76

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 01:14 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. overloaded >> operator issue...
    By soulredemption in forum C++ Programming
    Replies: 2
    Last Post: 10-17-2005, 10:53 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21