Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
4:     typedef unsigned short  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: }


Why is the this pointer used here??? Why can't just simply create the object temp like that?
C++
33:       Counter temp;



###############

Thanks for help all of you. But I have to admit that I still don't understand about it. Here are some some questions and thoughts of me...

C++
const Counter Counter::operator++(int)
    {

       ++itsVal;
       Counter temp(*this);
       return temp;
    }


1.Why is the argument of 'int' needed in this class method?
2.Why is the this pointer used creating counter temp? It looks for me like a parameter used in constructor or copy constructor, but the constructor sets only itsVal... Actually, the this pointer is a bit confusing thing. I understand only that the this pointer has the address of object working with.
3.Does const mean that the returning object is constant?



Edit (Bringing text written below by the autor as answer and adding the new questions here)
Posted
Updated 28-Feb-12 12:36pm

By passing this, the temp counter is initialized with the values of the object (itsVal) and the function returns a copy containing the state before incrementing. Creating temp without an argument will set itsVal to 0 (see the Counter::Counter() implementation).
 
Share this answer
 
It is because this is the iterator INT++ In that case you want to increase the value of INT but use the current value. For example, the code below will first output 1 and then 2.
int a = 1;
cout << a++;
cout << a;

Because of that the iterator needs to return the prior value at that point.

Good luck!
 
Share this answer
 
A variable which stores a reference to another variable is called a pointer. Pointers are said to "point to" the variable whose reference they store.

In this case the asterisk (*), acts as dereference operator and it's useful to think of it as moving context of the operation from the pointer to the variable it points to.

Consider the following:
C++
this->itsVal++; 
(*this).itsVal++; // dereference
Counter* ptr = this; // pointer declaration of ptr and assignment of this to ptr
ptr->itsVal++; 
(*ptr).itsVal++; // dereference


Best regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Feb-12 18:32pm    
Nice explanation, a 5.
I also would add that "this" is passed as yet another (hidden) parameter in all instance (non-static) functions, so those functions could have access to other instance members of the class's instance through this pointer. This is how instance method work and how they are different from static functions.
--SA
Espen Harlinn 29-Feb-12 17:02pm    
Good point :)
As Jochen Arndt said :

C#
const Counter Counter::operator++(int)
{
   Counter temp(*this);
   ++itsVal;
   return temp;
}


Just after Calling this Fucntion :
this-> itsVal will be equal to 1 but Temp->itsVal =0 and when u return temp you ll return an instance of the Counter with itsVal = 0. in the other hand if you ll return *this you ll be returning an instance with itsVal incremented by 1.

try some cases like :

C++
Counter Y = i++;


or

C++
const Counter Counter::operator++(int)
    {
       
       ++itsVal;
       Counter temp(*this);
       return temp;
    }
 
Share this answer
 
1.Why is the argument of 'int' needed in this class method?

The argument is needed to mark this operator as a postfix operator, and distinguish it from the corresponding prefix operator const Counter& Counter::operator++(). It's a dummy argument that is not used, or even initialized.

2.Why is the this pointer used creating counter temp?

Basically, the expected behaviour of the postfix increment operator is to increment its value, but return the value it had before incrementation. That is what this operator does with built-in types like int. Check the other solutions for explanations on the use of this.

3.Does const mean that the returning object is constant?

Yes. (Although I think it's rather pointless)
 
Share this answer
 
Because this is not a temporary object: it is a pointer to the instance of the class that is involved.

Think about cars:
My car.
Your car.

this car could be My car or Your car - it just depends on which one we are dealing with.

So you could say "start this car" and use the same procedure for both, or for Aunt Mabel's car, and for Brother Joe's car, and...
 
Share this answer
 
Comments
Espen Harlinn 28-Feb-12 10:45am    
That's one way to put it, but my guess is that he is confused by the asterisk as it is used to declare a pointer, dereference a pointer and multiplication ...

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