由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - overload "++i"里的operator“++”,怎么declare?
相关主题
operator++ 返回值问题member and friend
[C++] 入门级问题 increment and decrement operatorsAn object of A automatically converted to an object of B.
ambiguous operators in c++请问在class member function中如何调用overloaded function ca
有个SB interviewer和我说++i比i++好 (转载)C++: friend function
why use static function here?friend function 不能virtual 怎么搞呢?
operator overloading (C++)问个c++的弱问题
[合集] 又被羞辱了一把... (转载)还是咱们这儿,亲。
ostream& operator << (ostream& s, int cnt) errorOne question in C programming
相关话题的讨论汇总
话题: operator话题: overload话题: declare话题: name话题: function
进入Programming版参与讨论
1 (共1页)
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

1 (共1页)
进入Programming版参与讨论
相关主题
One question in C programmingwhy use static function here?
C++ func overload questionoperator overloading (C++)
对thinking in c++里这段代码没搞懂[合集] 又被羞辱了一把... (转载)
问个overloading new operator的问题ostream& operator << (ostream& s, int cnt) error
operator++ 返回值问题member and friend
[C++] 入门级问题 increment and decrement operatorsAn object of A automatically converted to an object of B.
ambiguous operators in c++请问在class member function中如何调用overloaded function ca
有个SB interviewer和我说++i比i++好 (转载)C++: friend function
相关话题的讨论汇总
话题: operator话题: overload话题: declare话题: name话题: function