如果((myptr)&&(myptr-> IsGreen())没有炸毁,你能做到吗? [英] can you do if((myptr) && (myptr->IsGreen()) without blowing up?

查看:51
本文介绍了如果((myptr)&&(myptr-> IsGreen())没有炸毁,你能做到吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我需要检查一个指针是否有效,我通常先做一个if(ptr)检查




但我看过这样的代码如下:


if((myptr)&&(myptr-> IsGreen())


这有效吗? br />

是因为评估是从左到右,所以首先评估if(ptr)位

。然后只有当指针有效才是正确的函数

被叫?

解决方案

6月20日,18:20,Angus< anguscom ... @ gmail.comwrote :


如果我需要检查指针是否有效我通常会先检查if(ptr)




但我看过这样的代码:


if((myptr)&&(myptr-> IsGreen())


这是有效的吗?



是的,这可以保证有效。


>

是因为评估是从左到右所以首先评估if(ptr)位

。 *那么只有当指针有效时才是正确的函数

调用?



这是标准所要求的,所有编译器都坚持

这个要求可以追溯到C.


我会避免使用指针并通过书面说明:if(myptr!= 0&&

myptr-> IsGreen()),但这是个人的选择。


/ Peter


Angus写道:


如果我需要检查一个指针是否有效,我通常先做一个if(ptr)检查




但我看过这样的代码:


if((myptr)&&(myptr-> IsGreen())


这有效吗?



是。


这是因为评估是从左到右,所以if(ptr)位
首先评估
。然后只有当指针有效才是正确的函数

调用?



正式理由是保证在[5.14 / 1]中:

&&操作员组从左到右。操作数都隐含地转换为bool类型(第4条)。如果两个操作数都是

为true,则结果为true,否则为false。不像&,&&保证从左到右

评估:如果第一个操作数是

false,则不评估第二个操作数。

Best


Kai-Uwe Bux


" Kai-Uwe Bux" < jk ******** @ gmx.netwrote in message

news:g3 ********** @ aioe.org ...
< blockquote class =post_quotes>
Angus写道:


>如果我需要检查一个指针是否有效我通常会做一个if(ptr)首先检查


但是我看过这样的代码:

if((myptr)&&(myptr-> IsGreen())

这是有效的吗?



是的。


>是吗因为评估是从左到右,所以首先评估if(ptr)位
。然后只有当指针有效才是正确的函数
调用?



正式原因是[5.14 / 1]中的保证:


&&&运算符组从左到右。操作数都是隐含的

转换为bool类型(第4条)。如果两个操作数



则为true,否则结果为true。与&,&&不同保证l eft-to-right

评估:如果第一个操作数为
false,则不评估第二个操作数。



注意,这有时被称为短路。如果(cond1 || cond2)

cond2仅在cond1为cond2时才会被评估false。


If I need to check if a pointer is valid I usually do a if(ptr) check
first.

But I have seen code such as this:

if((myptr) && (myptr->IsGreen())

Is this valid?

Is it because evaluation is from left to right so that the if(ptr) bit
is evaluated first. Then only if pointer is valid is right function
called?

解决方案

On 20 Jun., 18:20, Angus <anguscom...@gmail.comwrote:

If I need to check if a pointer is valid I usually do a if(ptr) check
first.

But I have seen code such as this:

if((myptr) && (myptr->IsGreen())

Is this valid?

Yes, this is guaranteed to work.

>
Is it because evaluation is from left to right so that the if(ptr) bit
is evaluated first. *Then only if pointer is valid is right function
called?

This is what is required in the standard, and all compilers adhere to
this requirement that dates back to C.

I would avoid the pointers and clarify by writing: if (myptr != 0 &&
myptr->IsGreen()), but this is a personal choice.

/Peter


Angus wrote:

If I need to check if a pointer is valid I usually do a if(ptr) check
first.

But I have seen code such as this:

if((myptr) && (myptr->IsGreen())

Is this valid?

Yes.

Is it because evaluation is from left to right so that the if(ptr) bit
is evaluated first. Then only if pointer is valid is right function
called?

The formal reason is the guarantee in [5.14/1]:

The && operator groups left-to-right. The operands are both implicitly
converted to type bool (clause 4). The result is true if both operands are
true and false otherwise. Unlike &, && guarantees left-to-right
evaluation: the second operand is not evaluated if the first operand is
false.
Best

Kai-Uwe Bux


"Kai-Uwe Bux" <jk********@gmx.netwrote in message
news:g3**********@aioe.org...

Angus wrote:

>If I need to check if a pointer is valid I usually do a if(ptr) check
first.

But I have seen code such as this:

if((myptr) && (myptr->IsGreen())

Is this valid?


Yes.

>Is it because evaluation is from left to right so that the if(ptr) bit
is evaluated first. Then only if pointer is valid is right function
called?


The formal reason is the guarantee in [5.14/1]:

The && operator groups left-to-right. The operands are both implicitly
converted to type bool (clause 4). The result is true if both operands
are
true and false otherwise. Unlike &, && guarantees left-to-right
evaluation: the second operand is not evaluated if the first operand is
false.

As a note, this is sometimes refered to as "short circuiting". It will also
work with ||
if ( cond1 || cond2 )

cond2 will only be evaluated if cond1 is false.


这篇关于如果((myptr)&amp;&amp;(myptr-&gt; IsGreen())没有炸毁,你能做到吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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