C++ 对象相等

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16843323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:41:27  来源:igfitidea点击:

C++ object equality

c++pointersequality

提问by user2399378

I have a class MyClothand one one object instance of that class that I instantiated like this:

我有一个类MyCloth和一个该类的一个对象实例,我像这样实例化:

MyCloth** cloth1;

And at one point in the program, I will do something like this:

在程序的某一时刻,我会做这样的事情:

MyCloth** cloth2 = cloth1;

And then at some point later, I want to check to see if cloth1and cloth2are the same. (Something like object equality in Java, only here, MyClothis a very complex class and I can''t build an isEqualfunction.)

然后在稍后的某个时候,我想检查一下cloth1cloth2是否相同。(类似于 Java 中的对象相等性,只有在这里,MyCloth是一个非常复杂的类,我无法构建isEqual函数。)

How can I do this equality check? I was thinking maybe checking if they point to the same addresses. Is that a good idea? If so, how do I do that?

我怎样才能做这个平等检查?我在想也许检查它们是否指向相同的地址。这是一个好主意吗?如果是这样,我该怎么做?

回答by Andy Thomas

You can test for object identityby comparing the addresses held by two pointers. You mention Java; this is similar to testing that two references are equal.

您可以通过比较两个指针持有的地址来测试对象身份。你提到了Java;这类似于测试两个引用是否相等。

MyCloth* pcloth1 = ...
MyCloth* pcloth2 = ...
if ( pcloth1 == pcloth2 ) {
    // Then both point at the same object.
}   

You can test for object equalityby comparing the contents of two objects. In C++, this is usually done by defining operator==.

您可以通过比较两个对象的内容来测试对象是否相等。在 C++ 中,这通常通过定义operator==.

class MyCloth {
   friend bool operator== (MyCloth & lhs, MyCloth & rhs );
   ...
};

bool operator== ( MyCloth & lhs, MyCloth & rhs )
{
   return ...
}

With operator== defined, you can compare equality:

定义 operator== 后,您可以比较相等性:

MyCloth cloth1 = ...
MyCloth cloth2 = ...
if ( cloth1 == cloth2 ) {
    // Then the two objects are considered to have equal values.
}   

回答by ChrisCM

If you would like to define a method by which to order a comparison of a set of objects of your custome class. For example:

如果您想定义一种方法,通过该方法对您的客户类的一组对象进行比较。例如:

someClass instance1;
someClass instance2;

You can do so by overloading the < operator for this class.

您可以通过重载此类的 < 运算符来实现。

class someClass
{

    bool operator<(someClass& other) const
    {
        //implement your ordering logic here
    }
};

If what you want to do is compare, and see if the objects are literally the same object, you can do a simple pointer comparison to see if they point to the same object. I think your question is poorly worded, I'm not sure which you're going for.

如果您想要做的是比较,并查看对象是否实际上是同一个对象,则可以进行简单的指针比较以查看它们是否指向同一个对象。我认为你的问题措辞不佳,我不确定你要问哪个。

EDIT:

编辑:

For the second method, it's really quite easy. You need access to the memory location of your object. You can access this in many different ways. Here are a few:

对于第二种方法,它真的很容易。您需要访问对象的内存位置。您可以通过多种不同的方式访问它。以下是一些:

class someClass
{

    bool operator==(someClass& other) const
    {
        if(this == &other) return true; //This is the pointer for 
        else return false;
    }
};

Note: I do not like the above, as usually == operators go more in depth than just comparing pointers. Objects can represent objects of similar qualities without being the same, but this is an option. YOu can also do this.

注意:我不喜欢上面的内容,因为通常 == 运算符比仅比较指针更深入。对象可以表示具有相似质量但不相同的对象,但这是一种选择。你也可以这样做。

someClass *instancePointer = new someClass();
someClass instanceVariable;
someClass *instanceVariablePointer = &instanceVariable;


instancePointer == instanceVariable;

This is non-sensical and invalid/false. If it would even compile, depending on your flags, hopefully your using flags that wouldn't allow this!

这是无意义的和无效/错误的。如果它甚至可以编译,取决于您的标志,希望您使用的标志不允许这样做!

instancePointer == &instanceVariable; 

This is valid and would result in false.

这是有效的,会导致错误。

instancePointer == instanceVaribalePointer;  

This is also valid and would result in false.

这也是有效的并且会导致错误。

instanceVariablePointer == &instanceVariable;

This is also valid and would result in TRUE

这也是有效的,将导致 TRUE

instanceVariable == *instanceVariablePointer;

This would use the == operator we defined above to get the result of TRUE;

这将使用我们上面定义的 == 运算符来获得 TRUE 的结果;