j ++的含义 [英] meaning of j++

查看:264
本文介绍了j ++的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始从Bruce Eckel的书Thinking in

C ++ 2 / e中学习C ++。 &安培;我试图理解j ++的含义。 ++ j是

对我来说很清楚。我使用的是g ++在我的Debian Sarge上。


这里有问题:


i = j = k = l = 0


++ i --- i = 0 ;;好的,好的


j ++ -j-- = 1 ;;什么?,为什么不呢0


k ++ -k ++ = 1 ;;什么?,为什么不呢。


l-- = -l-- = -1 ;;为什么不-2

不,我不是在谈论效率/优化。我正在谈论

我得到的答案。


我搜索了档案,但我得到的是关于使用

的优化++ j& j ++但不是关于这些的含义。有人有任何想法吗?布鲁斯没有在他的书中解释这种行为(除了一个

单句)


谢谢


" ; arnuld"

i have just started to learn C++ from Bruce Eckel''s book "Thinking in
C++ 2/e" & i am trying to understand the meaning of "j++". ++j is
pretty clear to me. i am using "g++" on my Debian Sarge.

here is the problem:

i = j = k = l = 0

++i ---i = 0 ;; OK, fine

j++ -j-- = 1 ;; WHAT?, why not 0

k++ -k++ = 1 ;; WHAT?, why not 2.

l-- = -l-- = -1 ;; why not -2
no i am not talking about efficiency/optimisation. i am talking about
the answers i got.

I searched archives but all i got is optimisations regarding the use of
++j & j++ but not regarding meaning of these. does anybody have any
idea? Bruce did not explain this behaviour in his book (except for a
single sentence)

thanks

"arnuld"

推荐答案

arnuld写道:
arnuld wrote :

j ++ -j - = 1 ;;什么?,为什么不0
j++ -j-- = 1 ;; WHAT?, why not 0



因为j--返回旧值,而不是新值。

because j-- returns the old value, not the new one.


>

k ++ -k ++ = 1 ;;什么?,为什么不2.
>
k++ -k++ = 1 ;; WHAT?, why not 2.



因为k ++返回旧值,而不是新值。

because k++ returns the old value, not the new one.

arnuld写道:
arnuld wrote:

i刚刚开始从Bruce Eckel的书思考中学习C ++
C ++ 2 / E" &安培;我试图理解j ++的含义。 ++ j是

对我来说很清楚。我使用的是g ++在我的Debian Sarge上。


这里有问题:


i = j = k = l = 0


++ i --- i = 0 ;;好的,好的


j ++ -j-- = 1 ;;什么?,为什么不呢0


k ++ -k ++ = 1 ;;什么?,为什么不呢。


l-- = -l-- = -1 ;;为什么不-2
i have just started to learn C++ from Bruce Eckel''s book "Thinking in
C++ 2/e" & i am trying to understand the meaning of "j++". ++j is
pretty clear to me. i am using "g++" on my Debian Sarge.

here is the problem:

i = j = k = l = 0

++i ---i = 0 ;; OK, fine

j++ -j-- = 1 ;; WHAT?, why not 0

k++ -k++ = 1 ;; WHAT?, why not 2.

l-- = -l-- = -1 ;; why not -2



我们在C ++语言新闻组。上面的文字不是C ++。关心

来解释一下你所说的那些有效的C ++令牌是什么意思混淆了

的奇怪订单?

We''re in a C++ language newsgroup. The text above is not C++. Care
to explain what it is you mean by all those valid C++ tokens mixed up
in a strange order?


[..]
[..]



V

-

请删除资金''A'' s通过电子邮件回复

我没有回复最热门的回复,请不要问

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask




arnuld写道:

arnuld wrote:

i刚刚开始从Bruce Eckel的书思考中学习C ++
C ++ 2 / E" &安培;我试图理解j ++的含义。 ++ j是

对我来说很清楚。我使用的是g ++在我的Debian Sarge上。


这里有问题:


i = j = k = l = 0


++ i --- i = 0 ;;好的,好的


j ++ -j-- = 1 ;;什么?,为什么不呢0


k ++ -k ++ = 1 ;;什么?,为什么不呢。


l-- = -l-- = -1 ;;为什么不-2


不,我不是在谈论效率/优化。我正在谈论

我得到的答案。


我搜索了档案,但我得到的是关于使用

的优化++ j& j ++但不是关于这些的含义。有人有任何想法吗?布鲁斯没有在他的书中解释这种行为(除了一个

单句)


谢谢


" ; arnuld"
i have just started to learn C++ from Bruce Eckel''s book "Thinking in
C++ 2/e" & i am trying to understand the meaning of "j++". ++j is
pretty clear to me. i am using "g++" on my Debian Sarge.

here is the problem:

i = j = k = l = 0

++i ---i = 0 ;; OK, fine

j++ -j-- = 1 ;; WHAT?, why not 0

k++ -k++ = 1 ;; WHAT?, why not 2.

l-- = -l-- = -1 ;; why not -2
no i am not talking about efficiency/optimisation. i am talking about
the answers i got.

I searched archives but all i got is optimisations regarding the use of
++j & j++ but not regarding meaning of these. does anybody have any
idea? Bruce did not explain this behaviour in his book (except for a
single sentence)

thanks

"arnuld"



这可能令人困惑,但非常简单!


像j ++一样的后增量运算符映射到这样的函数:


int operator ++()

{

int tmp = j

j + = 1;

返回tmp;


}


前增量运算符映射到这样的函数:

int operator ++(int)

{

j + = 1;

返回j;

}


如果功能名称混淆了你不注意它们,但是看看

他们做了什么。

所以j ++返回j的旧值,但是j增加1,而++ j

先增加然后返回j的值。

-Thomas

This can be confusing, but is really easy!

The post increment operator like j++ maps to a funtion like this:

int operator++()
{
int tmp = j
j += 1;
return tmp;

}

And the pre incremend operator maps to a function like this:
int operator++(int)
{
j += 1;
return j;
}

If the function names confuses you don''t pay attention to them, but see
what they do.

So j++ return the old value of j , but increases j by 1, and ++j
increases first and then return the value of j.
-Thomas


这篇关于j ++的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆