1: // Listing 10.12
2: // Returning the dereferenced this pointer
3:
4: typedefunsignedshort USHORT;
5: #include <iostream.h>
6:
7: class Counter
8: {
9: public:
10: Counter();
11: ~Counter(){}
12: USHORT GetItsVal()const { return itsVal; }
13: void SetItsVal(USHORT x) {itsVal = x; }
14: const Counter& operator++ (); // prefix
15: const Counter operator++ (int); // postfix
16:
17: private:
18: USHORT itsVal;
19: };
20:
21: Counter::Counter():
22: itsVal(0)
23: {}
24:
25: const Counter& Counter::operator++()
26: {
27: ++itsVal;
28: return *this;
29: }
30:
31: const Counter Counter::operator++(int)
32: {
33: Counter temp(*this);
34: ++itsVal;
35: return temp;
36: }
37:
38: int main()
39: {
40: Counter i;
41: cout << "The value of i is " << i.GetItsVal() << endl;
42: i++;
43: cout << "The value of i is " << i.GetItsVal() << endl;
44: ++i;
45: cout << "The value of i is " << i.GetItsVal() << endl;
46: Counter a = ++i;
47: cout << "The value of a: " << a.GetItsVal();
48: cout << " and i: " << i.GetItsVal() << endl;
49: a = i++;
50: cout << "The value of a: " << a.GetItsVal();
51: cout << " and i: " << i.GetItsVal() << endl;
52: return 0;
53: }
Output: The value of i is 0
The value of i is 1
The value of i is 2
The value of a: 3 and i: 3
The value of a: 3 and i: 4
I do not understand how the function for overloading the postfix++ operator works here - lines 31-36
More specifically this line:Counter temp(*this);
What does this line do?Is temp() a built in function?
what I don't understand here is that since we are calling ++ on an object and we have overloaded it, how does compiler know that it has to call the postfix function for a = i++; and prefix function for a = ++i;. can anyone please explain
let me elaborate my question. In the case of primitives it is clear the ++ before the primitive is treated as prefix and after the primitive is treated as postfix. But this is a user defined object. We have defined two functions operator++ . How does compiler brand one as "prefix" and other as "postfix"?
or
why are these true?
25: const Counter& Counter::operator++() //PREFIX - notice that this one takes no parameters
26: {
27: ++itsVal;
28: return *this;
29: }
30:
31: const Counter Counter::operator++(int) //POSTFIX - this one has an UNUSED dummy int parameter.
32: {
33: Counter temp(*this);
34: ++itsVal;
35: return temp;
36: }
When the compiler needs the postfix ++ function - it uses the one which has been defined to take an int parameter.
( It does not actually use the parameter though)
The same thing applies to the decrement operator --.
ok, so the answer is that the compiler has the same signature for a prefix and postfix operators for the user-defined objects as for the primitive objects. So, I cannot have a function like operator++(float). Thanks guys! I understand it now
On a slightly related topic, how does one make i++++ work?
With the code from above, the compiler complains: passing const Counter as this argument of const Counter& Counter::operator++() discards qualifiers
Getting rid of the Const solves that but when using a = ++++i; and a = i++++; the output is
1 2 3 4 5
The value of i is 0
The value of i is 1
The value of i is 2
The value of a: 4 and i: 4
The value of a: 4 and i: 5
when the last line should be The value of a: 4 and i: 6
i += 2;
You can't/shouldn't use more then one ++ or -- operator in a single expression because there is no standard to declare in what order the operators must evaluate.
if you overload an unary operator with no arguments, compiler does prefix,
with an argument, it does postfix. any kind of argument will be a sign to the compiler to do postfix.