How to compare two classes instances
-
How i campare two objects, such that if i have class(given below) and i want to compare there two different instances then how ??
class MyNode {
MyNode left;
MyNode right;
}Is there any possiblity to do that --->
public boolean MyFunction(MyNode toSearch, MyNode parentNode) {
MyNode currentNode = parentNode;
while(currentNode != toSearch) {
return true;
}
else {
currentNode = currentNode.left;
}
}
return false; -
How i campare two objects, such that if i have class(given below) and i want to compare there two different instances then how ??
class MyNode {
MyNode left;
MyNode right;
}Is there any possiblity to do that --->
public boolean MyFunction(MyNode toSearch, MyNode parentNode) {
MyNode currentNode = parentNode;
while(currentNode != toSearch) {
return true;
}
else {
currentNode = currentNode.left;
}
}
return false; -
You should implement your own
Equals()
method in your class and use that to test for equality. Using the==
and!=
operators will not give the result that you are looking for.I must get a clever new signature for 2011.
but can you please give some guide line to do this ... such that one way in my mind is like this that i put an
Interger
property in class that are then use to identify different instanceof class .. such that1
for 1st instance/node then2
for 2nd and so on and then compare some thing like thisNodeA.IntegerIdentifier == NodeB.IntegerIdentifier
but i doesn't satisfy with this idea as it is not apply in real senario as because in programming we should try to make solution in some thing like general awy. So is there any idea aur suggestion for me that how i achieve this equality problem/isue what ever :) -
but can you please give some guide line to do this ... such that one way in my mind is like this that i put an
Interger
property in class that are then use to identify different instanceof class .. such that1
for 1st instance/node then2
for 2nd and so on and then compare some thing like thisNodeA.IntegerIdentifier == NodeB.IntegerIdentifier
but i doesn't satisfy with this idea as it is not apply in real senario as because in programming we should try to make solution in some thing like general awy. So is there any idea aur suggestion for me that how i achieve this equality problem/isue what ever :) -
How i campare two objects, such that if i have class(given below) and i want to compare there two different instances then how ??
class MyNode {
MyNode left;
MyNode right;
}Is there any possiblity to do that --->
public boolean MyFunction(MyNode toSearch, MyNode parentNode) {
MyNode currentNode = parentNode;
while(currentNode != toSearch) {
return true;
}
else {
currentNode = currentNode.left;
}
}
return false;khurram_shahzad wrote:
and i want to compare there two different instances then how ??
Your question is not really clear. There's a difference between having the same instance and having two different instances which are the same. In the first case, you want to verify if you are talking about the same instance. That's what you do in the code sample you posted: you check wether
currentNode
is the same object astoSearch
. In the second case, you want to verify that two instances contain the same thing but they are most of the time two different objects. In your case, from what I saw in your code sample, you want to verify whether you are talking about the same object. Thus, it is fine to write it the way you did and it will work fine (in fact, when you use the == operator with objects, it will compare if the adresses of the two objects are the same, thus return true if the two pointers point to the same class instance).Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
You should implement your own
Equals()
method in your class and use that to test for equality. Using the==
and!=
operators will not give the result that you are looking for.I must get a clever new signature for 2011.
I think the OP wanted to search for a specific class instance in a linked list (at least, that's what I guessed from his code snippet). Thus, his code using the == operator was fine.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
I think the OP wanted to search for a specific class instance in a linked list (at least, that's what I guessed from his code snippet). Thus, his code using the == operator was fine.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
I think the OP wanted to search for a specific class instance in a linked list (at least, that's what I guessed from his code snippet). Thus, his code using the == operator was fine.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++Thanks A LLLot .... Thanks for your great help ....
-
khurram_shahzad wrote:
and i want to compare there two different instances then how ??
Your question is not really clear. There's a difference between having the same instance and having two different instances which are the same. In the first case, you want to verify if you are talking about the same instance. That's what you do in the code sample you posted: you check wether
currentNode
is the same object astoSearch
. In the second case, you want to verify that two instances contain the same thing but they are most of the time two different objects. In your case, from what I saw in your code sample, you want to verify whether you are talking about the same object. Thus, it is fine to write it the way you did and it will work fine (in fact, when you use the == operator with objects, it will compare if the adresses of the two objects are the same, thus return true if the two pointers point to the same class instance).Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++Yes here im comparing to diffrent instace of same class ..... Thanks A LLLot .... Thanks for your great help .... :-D