Original Post
I was reading the advanced functions chapter in Sams Teach Yourself C++ in 21 days today, and I came across the section on returning the this pointer, this is the source code for it:
//a program to demenstrate overloading the prefix operator
#include <iostream>
using namespace std;
class Counter
{
public:
Counter();
~Counter(){}
int GetItsVal() const
{
return itsVal;
}
void Increment()
{
itsVal++;
}
const Counter& operator++ ();
private:
int itsVal;
};
Counter::Counter(): itsVal(0)
{}
const Counter& Counter:: operator++ ()
{
itsVal++;
return *this;
}
int main()
{
Counter i;
cout << "The Value of i is " << i.GetItsVal() << endl;
i.Increment();
cout << "The value of i is " << i.GetItsVal() << endl;
++i;
cout << "The value of i is " << i.GetItsVal() << endl;
Counter a = ++i;
cout << "The value of a: " << a.GetItsVal() << endl;
cout << "and i: " << i.GetItsVal() << endl;
return 0;
}
And I was wondering why in the operator prototype it shows that it returns a reference, but it really returns a pointer?