Hi all,
I just reached the overloading chapter, amazing and confusing .
so i tried to overload the pre and post increment operations. So i did the following:
Simple class:
Code:
class Counter {
public:
Counter() : val(0) {};
Counter(int ival) : val(ival) {};
Counter(const Counter& rhs);
int GetVal() const { return val; }
void SetVal(int v) { val = v; }
const Counter operator++();
const Counter operator++(int dummy);
private:
int val;
};
Pre-increment:
Code:
const Counter Counter::operator++() {
cout << "pre increment called" << std::endl;
++val;
return *this;
}
Post-increment:
Code:
const Counter Counter::operator++(int dummy) {
cout << "post increment called" << std::endl;
Counter temp(*this);
++val;
return temp;
}
using both of them :
Code:
Counter c(1);
Counter pre = ++c;
Counter post = c++;
i printed the results. they are fine and correct.
the 2 functions are similar except the parameter "dummy".
my question : how is ++c or c++ calling the right function ?
why is ++c calling the function without input parameter? and c++ calling the function with the input parameter?