标题:[求助]++a++与(++a)++
只看楼主
ioriliao
Rank: 7Rank: 7Rank: 7
来 自:广东
等 级:贵宾
威 望:32
帖 子:2829
专家分:647
注 册:2006-11-30
结帖率:78.95%
 问题点数:0 回复次数:46 
[求助]++a++与(++a)++
++a++这样不行,为什么(++a)++ 行呢?
谢谢.
搜索更多相关主题的帖子: 为什么 
2007-06-15 11:18
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
得分:0 

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;


editted away since it is very hard to understand.

[此贴子已经被作者于2007-6-15 11:46:56编辑过]


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-06-15 11:33
ioriliao
Rank: 7Rank: 7Rank: 7
来 自:广东
等 级:贵宾
威 望:32
帖 子:2829
专家分:647
注 册:2006-11-30
得分:0 
仁兄,你的程序我看不明白,我希望简单点.

/images/2011/147787/2011051411021524.jpg" border="0" />
2007-06-15 11:38
anthony634
Rank: 6Rank: 6
来 自:西南交大
等 级:贵宾
威 望:24
帖 子:653
专家分:10
注 册:2006-6-8
得分:0 
从左向右尽可能多地把连续的+号组成运算符。所以把自己搞糊涂了
2007-06-15 11:53
ioriliao
Rank: 7Rank: 7Rank: 7
来 自:广东
等 级:贵宾
威 望:32
帖 子:2829
专家分:647
注 册:2006-11-30
得分:0 
以下是引用HJin在2007-6-15 11:33:01的发言:

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;


editted away since it is very hard to understand.

仁兄的意思是++a 返回的变量,a++返回的是常量是不,但为什么会有这种区别的,两者都是自增运算符,为什么会有这种区别?


/images/2011/147787/2011051411021524.jpg" border="0" />
2007-06-15 12:24
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
得分:0 

Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-06-15 12:49
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
得分:0 
我觉得这要看编译器是怎么解析的,
前自增和后自增的优先级一样,如果先前自增的话,我觉得上面那个可以!
如果先后自增的话,就不对了!

不知道对不!

Fight  to win  or  die...
2007-06-15 12:52
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
得分:0 
++a return (a+1)
a++ return (b=a,a+1,b)

Fight  to win  or  die...
2007-06-15 13:07
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
得分:0 

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编辑过]


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-06-15 13:36
ioriliao
Rank: 7Rank: 7Rank: 7
来 自:广东
等 级:贵宾
威 望:32
帖 子:2829
专家分:647
注 册:2006-11-30
得分:0 
谢谢各位的帮助,我似乎明白了,我要去做多些测试看看才行!

/images/2011/147787/2011051411021524.jpg" border="0" />
2007-06-15 15:02



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-147663-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.292670 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved