我该如何检查相同的物体 [英] How Should I Check Equal Objects

查看:61
本文介绍了我该如何检查相同的物体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该如何正确地写这个if语句?



how should i write this if statement correctly?

class state{
    public:
         state* start;
         state* now;
        bool equal(){
                  if(state.now==state.start)
                  return true;
}

};

推荐答案

这取决于你要测试的内容。 />
从您的代码结果中,您要测试两个指针指向同一个对象,在这种情况下,等于方法的正确代码是下一个:



This depends on what you whant to test.
From your code results that you want to test that both pointers point to the same object, in this case the correct code for equal method is the next one:

bool equal()
{
   return (now == start);
}





但是如果你需要别的东西就可以问一下!



因此,通过查看新发布的代码(作为对我的解决方案的评论),应该修改相同的方法:



But if you need something else just ask!

So by seeing your new posted code (as comment to my solution) the equal method should be modified like this:

bool state::equal(state* a, state *b)
{
   return ((a == null && b == null) ||  (a!= null && b != null && a->now == b->now && a->start == b->start));
}


这篇关于我该如何检查相同的物体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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