不是空的 [英] not null

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

问题描述




以下代码已完成且在一个班级内工作;但是,它使用

e作为Entry对象的指针,而不检查它是否为空。

这当然无关紧要是否创建了对象;但是,如果e,那么
可能会引起问题。是空的。是否有一个简单的

意味着我可以用来检查e是不是空?


感谢您的帮助


//找到胜利者


无效种族::赢家(ostream& out)const


{


条目* e = NULL;


if(nEntries == 0)


{


out<< 没有条目 <<结束;


}


其他


{


bool found_a_winner = false; //没有找到胜利者




for(int i = 0; i< nEntries; i ++)


{


e = entries [i];


if(e-> getPlace()== 1)
< br $>
{


out<< 获胜者是: << endl;


out<< * e;


found_a_winner = true; //赢家已经找到了
找到了


}


}


if(!found_a_winner)//如果没有赢家


{


out<< 在这场

比赛中没有找到胜利者 <<结束;


}


}


}



The following code is complete and working within a class; however, it uses
"e" as a pointer to an Entry object without checking if that it is not null.
This of course doesn''t matter if objects are created correctly; however, it
will probably cause a problem should the "e" be null. Is there a simple
means I can use to check that "e" is not null?

Thanks for any help

//Find the winner

void Race::winner (ostream& out) const

{

Entry* e = NULL;

if (nEntries == 0)

{

out << "No entries" << endl;

}

else

{

bool found_a_winner = false; // no winner found
yet

for (int i=0; i<nEntries; i++)

{

e = entries[i];

if (e->getPlace() == 1)

{

out << "The winner is: " << endl;

out << *e;

found_a_winner = true; // a winner has been
found

}

}

if (!found_a_winner) // if no winner

{

out << "No winner was found in this
race" << endl;

}

}

}

推荐答案



" John J" < ... @ ...>在消息中写道

news:d4 ****************************** @ news.teranew s。 com ...

"John J" <...@...> wrote in message
news:d4******************************@news.teranew s.com...


以下代码已完成并在一个类中工作;但是,
使用e。作为Entry对象的指针,而不检查它是否不是
null。这当然是否正确创建对象并不重要;但是,如果e,它可能会引起问题。是空的。是否有一个简单的方法可以用来检查e是不是空?


The following code is complete and working within a class; however, it uses "e" as a pointer to an Entry object without checking if that it is not null. This of course doesn''t matter if objects are created correctly; however, it will probably cause a problem should the "e" be null. Is there a simple
means I can use to check that "e" is not null?




是你之前的代码


if(e)

{

// e不是空的

}

其他

{

// e为空

}


完全正确。不过我会说这不是对它进行测试的正确位置。创建对象时,您应该检查是否正确创建了它。一旦发生任何错误就会报告任何错误。


john



Yes the code you had before

if (e)
{
// e is not null
}
else
{
// e is null
}

was perfectly correct. However I would say this isn''t the right place to
test for it. It is when the object is created that you should be checking if
it is created correctly. The way you can report any error as soon as it
happens.

john


John哈里森写道:
John Harrison wrote:

if if(e)
{e /> // e is not null

if (e)
{
// e is not null




或者甚至

if(e!= NULL){

...

}

当然是相当于(并且更清晰)

if(e!= 0){

...

}

问候,

雅克。



Or even
if (e != NULL){
...
}
which is of course equivalent to (and clearer than)
if (e != 0){
...
}

Regards,
Jacques.


Jacques Labuschagne写道:
Jacques Labuschagne wrote:
甚至
if(e!= NULL){
...
}
这当然等于(并且更清晰)
if(e!= 0){
...
}
Or even
if (e != NULL){
...
}
which is of course equivalent to (and clearer than)
if (e != 0){
...
}




NULL来自某些C包含,并且不是

语言的基本部分。 0是正确的。



NULL is from some of the C includes and is not a base part of the
language. 0 is correct.


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

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