G*******n 发帖数: 2041 | 1 看到i++和++i的讨论,想到如果要overload他们,怎样declare才能把它们区分开呢? |
u****z 发帖数: 43 | 2 class Number {
public:
Number& operator++ (); // prefix ++
Number operator++ (int); // postfix ++
};
【在 G*******n 的大作中提到】 : 看到i++和++i的讨论,想到如果要overload他们,怎样declare才能把它们区分开呢?
|
i***h 发帖数: 12655 | 3 one of them has a dummy arg |
f*****Q 发帖数: 1912 | 4 ++i
=================
I I:: operator++(){
/*
Do whatever you want.
*/
return *this;
}
=================
i++
==================
I I::operator++(int){//Don't ask me why, I don't know
I r = *this;
/*
Do whatever you want.
*/
return r;
}
===================
有高手给解释一下为什么吗? |
i***h 发帖数: 12655 | 5 It's a dummy arg, doesn't have a name so can't be used in the function.
Just a trick to tell compiler this is a different function from the prefix
version. |
t****t 发帖数: 6806 | 6 err, strictly speaking, even it is a "dummy" arg, it can still have a name (
although most ppl don't give a name). AND it will be called with value 0.
but that doesn't really matter...
【在 i***h 的大作中提到】 : It's a dummy arg, doesn't have a name so can't be used in the function. : Just a trick to tell compiler this is a different function from the prefix : version.
|
i***h 发帖数: 12655 | 7
(
You are right. I was referring to the code example upstairs, which does not
have a name
AND it will be called with value 0.
A question, if it does not have a name, is the compiler smart enough not to
create storage for it on the function stack? Thanks
【在 t****t 的大作中提到】 : err, strictly speaking, even it is a "dummy" arg, it can still have a name ( : although most ppl don't give a name). AND it will be called with value 0. : but that doesn't really matter...
|
t****t 发帖数: 6806 | 8 if the function is inlined, then yes. if not, probably not (i am not sure).
not
to
【在 i***h 的大作中提到】 : : ( : You are right. I was referring to the code example upstairs, which does not : have a name : AND it will be called with value 0. : A question, if it does not have a name, is the compiler smart enough not to : create storage for it on the function stack? Thanks
|