C ++ object equality

I have a MyCloth class and one instance of the object of this class that I created this way:

 MyCloth** cloth1; 

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

 MyCloth** cloth2 = cloth1; 

And then, at some point, I want to check if the tags cloth1 and cloth2 . (Something like object equality in Java, only here MyCloth is a very complex class, and I cannot build the isEqual function.)

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

+6
source share
2 answers

You can check the identifier of an object by comparing the addresses held by two pointers. You mention Java; it is like testing that two links are equal.

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

You can verify the equality of an object by comparing the contents of two objects. In C ++, this is usually done by defining operator== .

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

With a specific == operator, you can compare the equality:

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

If you want to define a method with which you can order a comparison of the set of objects of your class custome. For instance:

 someClass instance1; someClass instance2; 

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

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

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

EDIT:

For the second method, this is very easy. You need access to the location of your property. You can access this in many 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 don't like the above, since usually == statements go deeper than just comparing pointers. Objects can represent objects of the same 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; 

It is insensitive and invalid / false. If it even compiled, depending on your flags, I hope you use flags that would not allow this!

 instancePointer == &instanceVariable; 

This is valid and will result in false.

 instancePointer == instanceVaribalePointer; 

This is also true and will lead to false.

 instanceVariablePointer == &instanceVariable; 

This is also true and will result in TRUE

 instanceVariable == *instanceVariablePointer; 

This will use the == operator, which we defined above, to get the result TRUE;

+3
source

Source: https://habr.com/ru/post/946202/


All Articles