Advertisement

overloading operators

Started by August 01, 2002 06:21 PM
2 comments, last by viper35al 22 years, 5 months ago
Ok, I''m lost with the following code in my book, please help. Questions are in code area.
  // Listing 10.12

// Returning the dereferenced this pointer


#include <iostream.h>

class Counter
{
public:
	Counter();
	~Counter(){}
	int GetItsVal()const { return itsVal; }
	void SetItsVal(int x) {itsVal = x; }
	const Counter& operator++ ();      // prefix

	const Counter operator++ (int); // postfix


private:
	int itsVal;
};

Counter::Counter():
itsVal(0)
{}

const Counter& Counter::operator++()
{
	++itsVal;
	return *this; // ????

}

const Counter Counter::operator++(int x)// how does the compiler

// know that this is postfix and not prefix?

{
	Counter temp(*this);//what does this do?

	++itsVal;
	return temp;
}

int main()
{
	Counter i;
	cout << "The value of i is " << i.GetItsVal() << endl;
	i++;
	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();
	cout << " and i: " << i.GetItsVal() << endl;
	a = i++;
	cout << "The value of a: " << a.GetItsVal();
	cout << " and i: " << i.GetItsVal() << endl;
	return 0;
}

  
Thanks.
this is special pointer point to the OBJECT whose member function excuted belongs to. Imagine: There are many Object but there is only one common member function in a class, that member function declare in a class without giving it the name of object it belongs to. We can have many Object in a program. So when a member function called by using this pointer it can know which Object own that member function.
So return *this = return the object that call this member function
Hope it help
Advertisement
"return *this"
That returns a reference to the current Counter, which allows you to use code such as "a = ++b;". If, for example, you changed the return type of operator++() to void, then "a = ++b;" would be a compile-time error.

"how does the compiler know that this is postfix and not prefix?"
It''s like if you have two separate functions: f() and f(int). The compiler can tell the difference between those, and it can do the same for operator++() and operator++(int). The int in operator++(int) isn''t actually used, it''s just there so the compiler knows the difference.

"Counter temp(*this);"
That creates a Counter on the stack called "temp", calling the copy constructor (which makes "temp" equal to the current Counter).


I hope I haven''t been too obscure
Exactly Beer Hunter.
quote:
"Counter temp(*this);"

is the same as
Counter temp = *this
since "*this" is an object of Counter class, The compiler invoke default copy constructor to create another copy of "*this" object (The object that the "this" pointer points to) and temp receives that copy.
But note that, it works fine because Counter does not have POINTER in it. But it is not a recommended method to let the compiler call copy constructor for us when Counter class has Pointer because that time Both of source and destination object of Counter class will exactly the same even the address of memory pointer variables point to (You know how dangerous pointers are). Beer Hunter, I bet you totally understand what i mean!?. More information about this can be found any tutorial about "Copy constructor" topic

This topic is closed to new replies.

Advertisement