Comparing object instances

  • Thread starter Thread starter Abubakar
  • Start date Start date
A

Abubakar

Hi,

(using native/unmanaged vc++ 2k5)....

How do I compare one object instance to another.....
like in C# *is* operator is used... how is it done in native c++.

Regards,

Ab.
 
Hi,
(using native/unmanaged vc++ 2k5)....

How do I compare one object instance to another.....
like in C# *is* operator is used... how is it done in native c++.

Regards,

Hi,

To be able to compare 2 object instances you can do this:
class MyClass
{
int a;
public:
MyClass(int A)
{
a = A;
}
bool operator==(const MyClass& B)
{
if(B.a == this->a)
return true;
else
return false;
}
bool operator!=(const MyClass& B)
{
if(B.a != this->a)
return true;
else
return false;
}
};

and to test it:
MyClass A(0);
MyClass B(1);
MyClass C(1);

if(A != B)
{
printf("A != B\n");
}
if(B == C)
{
printf("B == C\n");
}

--

Kind regards,
Bruno van Dooren
(e-mail address removed)
Remove only "_nos_pam"
 
Back
Top