B
Bo Sun
hi:
in the following code:
class plus{
int data_item;
public:
plus(int val){ data_item = val;}
plus operator++(){
data_item ++;
return *this;
}
plus operator++(int val) {
data_item += val;
return *this;
}
};
....
plus a(10), b(20);
++a;
b++;
I have the following questions:
1) why ++a will invoke plus operator++()? why b++ will invoke plus
operator++(int val)?
2) in the overloading of operator ++(int val), val is not used. Suppose I
want to make data_item increase by val, like the definition of ++(int
val), how can I operate on b? b++(10) does not work.
many thanks,
bo
in the following code:
class plus{
int data_item;
public:
plus(int val){ data_item = val;}
plus operator++(){
data_item ++;
return *this;
}
plus operator++(int val) {
data_item += val;
return *this;
}
};
....
plus a(10), b(20);
++a;
b++;
I have the following questions:
1) why ++a will invoke plus operator++()? why b++ will invoke plus
operator++(int val)?
2) in the overloading of operator ++(int val), val is not used. Suppose I
want to make data_item increase by val, like the definition of ++(int
val), how can I operate on b? b++(10) does not work.
many thanks,
bo