谢谢.
谢谢.
1. a++ returns a const obj
++a returns a reference.
see the following code --- how to overload ++ and -- in C++.
2. ++a++ does not work since your complier interprets it as
++( (a++) )
Since a++ is a const obj, say b, ++b must not be allowed;
[此贴子已经被作者于2007-6-15 11:46:56编辑过]
1. a++ returns a const obj
++a returns a reference.
see the following code --- how to overload ++ and -- in C++.
2. ++a++ does not work since your complier interprets it as
++( (a++) )
Since a++ is a const obj, say b, ++b must not be allowed;
仁兄的意思是++a 返回的变量,a++返回的是常量是不,但为什么会有这种区别的,两者都是自增运算符,为什么会有这种区别?
have your ever overloaded the ++ and -- (totally 4 operators) for a class, say A? If you did this, then you understand what I said.
Try to do the following exercise:
[CODE]class A
{
// overload ++(two versions)
private:
int d;
double d;
};[/CODE]
your declaration for the postfix ++ must be
[CODE]const A operator++(int); /// postfix [/CODE]
(here we used a dummy int argument to mark it as the postfix version, otherwise, you cannot
make it different from the prefix version. )
for the prefix version must be
[CODE]A& operator++() /// prefix[/CODE]
All other variants are wrong. You may ask me why this has to be so? I would have to tell you, the writers for the compliers, both MS C++ and g++, implemented it so.
/**
This is an option for the postfix ++. But since it returns void,
we cannot use it in an expression, say (a++) + 1.
*/
//void operator++(int)
//{
// ++(*this);
//}
[此贴子已经被作者于2007-6-15 13:40:56编辑过]