Click here to Skip to main content
15,896,313 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi In this code when we use ++ as a postfix operator then why we have to pass the temp variable in the postfix operator function?

C++
#include<iostream.h>
#include<conio.h>

class student
{
      int sub1;
      int sub2;
      
      public:
             student()
             {
                      sub1=0;
                      sub2=0;
             }
             
             void setdata(int tsub1,int tsub2)
             {
                  sub1=tsub1;
                  sub2=tsub2;
             }
             
             void display()
             {
                  cout<<"\n Sub1 is: "<<sub1;
                  cout<<"\n Sub2 is: "<<sub2;
             }
             
                          
             void operator++(int temp)
             {
                  sub1=sub1*5;
                  sub2=sub2*5;
             }
             
};

  int main()
  {
      student obj1;
      obj1.setdata(5,5);
      obj1.display();
      obj1++;
      obj1.display();
      getch();
      return 0;
  }


[edit]Code block sorted, spurious closing tags removed - OriginalGriff[/edit]
Posted
Updated 24-Jun-11 4:14am
v2
Comments
Philippe Mori 24-Jun-11 11:03am    
By the way your signature is not standard... Thus it will not be possible to chain operators as with built-in type. As muich as possible, when operators are overloadded they should have a semantic similar to built-in types.

Also, most of the time, prefix oprator should be prefered over postfix. This is particulary the case when using STL iterators to avoid as much as possible useless ooverhead and to follow the conventions.

That is to differentiate the postfix version (st++) from than the prefix one (++st), as described here[^].
 
Share this answer
 
Comments
Manfred Rudolf Bihy 24-Jun-11 10:51am    
Good link! 5+
Richard MacCutchan 24-Jun-11 11:03am    
It's amazing what you can find if you try!
Manfred Rudolf Bihy 24-Jun-11 11:33am    
:)
Albert Holguin 24-Jun-11 13:15pm    
my 5
First see Richard's[^] solution. Then read on here why what you're doing is dangerous :).
The semantic of postfix ++ is that the expression result of xxx++ is the yet unmodified entity xxx. Since your ++ operator does not return a clone of the not yet modified student instance you should stick to using the prefix ++ operator. Using a postfix ++ without using the result (which your ++ does not even possess) is the same thing as using prefix ++. That of course is only half the truth as you could easily have postfix ++ and prefix ++ do completely different stuff computational wise, but that is also of the big no-nos ;).

If you have any doubts leave a comment.

Cheers!

--MRB
 
Share this answer
 
Comments
fjdiewornncalwe 24-Jun-11 10:48am    
I know this, but I'm not sure I could have encapsulated that explanation as precisely as you have here. +5.
Manfred Rudolf Bihy 24-Jun-11 10:53am    
Thanks Marcus!
Richard MacCutchan 24-Jun-11 11:04am    
Excellent description of the issue. I ignored the fact that OP's overload served no sensible purpose.
Stefan_Lang 27-Jun-11 6:43am    
Absolutely correct and my 5 for the excellent description.

Just wanted to add that it can occasionally make sense to define an operator in a way that does not macth with the expected semantics, for instance for an algebra library I defined the operator^ to calculate the vector product of two 3D-vectors. In this case it was safe to do so since there is no sensible definition for "to the power of" for 3D vectors which you might semantically associate with the operator symbol '^'.
Postfix operator should be defined like this :

C#
student operator++(int unused)  // postfix
{
    // make a copy using copy constructor
    student old(*this); 

    // update original object
    sub1=sub1*5;
    sub2=sub2*5;

    // return the copy
    return old;
}


Now we can see why postfix operator should not be used when the old value is not used. This can hinder performance if the object is complex to copy.

By the way, with this version if you write (obj1++).display(), the old value will be printed as expected (as it would also be the case with built-in types like integers).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900