I have a class MyCloth
and one one object instance of that class that I instantiated like this:
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 cloth1
and cloth2
are the same. (Something like object equality in Java, only here, MyCloth
is a very complex class and I can''t build an isEqual
function.)
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?
In C#, we can check if two object values are equal with the Equals() method. This method is used to get a Boolean value that indicates whether or not a certain value is equal to another.
Both operands of any relational or equality operator can be pointers to the same type. For the equality ( == ) and inequality ( != ) operators, the result of the comparison indicates whether the two pointers address the same memory location.
The == operator checks if two values are equal. The != operator checks if two values are not equal. It is also known as the loose equality operator because it checks abstract equality, i.e., it tends to convert the data types of operands in order to carry the comparison when two operands aren't of the same data type.
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
instanceVariable == *instanceVariablePointer;
This would use the == operator we defined above to get the result of TRUE;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With