Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / April 2006

Tip: Looking for answers? Try searching our database.

C++/CLI comparing two clr poiinters for equality

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Edward Diener - 24 Apr 2006 18:46 GMT
Now that operator overloading allows to ref classes to be compared for
equality using == syntax, how does one compare the actual ref pointers (
^ ) for equality instead ?

As an example:

SomeRefObject ^ obj1(..initialized somehow);
SomeRefObject ^ obj2(..initialized somehow);

if (obj1 == obj2) // This compares the objects themselves for equality
in C++/CLI I assume. How do I compare the ref pointers themselves ( ^ )
for equality ?
Tamas Demjen - 24 Apr 2006 19:16 GMT
> Now that operator overloading allows to ref classes to be compared for
> equality using == syntax, how does one compare the actual ref pointers (
> ^ ) for equality instead ?

I think "Equals" is the answer. It compares the addresses of two objects:
http://msdn2.microsoft.com/en-us/library/system.object.equals.aspx

Tom
Marcus Heege - 24 Apr 2006 21:15 GMT
> Now that operator overloading allows to ref classes to be compared for
> equality using == syntax, how does one compare the actual ref pointers (
[quoted text clipped - 7 lines]
> if (obj1 == obj2) // This compares the objects themselves for equality in
> C++/CLI I assume.

If SomeRefObject overwrites Equals, yes, otherwise no

> How do I compare the ref pointers themselves ( ^ ) for equality ?

Object::ReferenceEquals(obj1, obj2)
Edward Diener - 24 Apr 2006 23:00 GMT
>> Now that operator overloading allows to ref classes to be compared for
>> equality using == syntax, how does one compare the actual ref pointers (
[quoted text clipped - 9 lines]
>
> If SomeRefObject overwrites Equals, yes, otherwise no

So are you saying that operator overloading in C++/CLI for the ==
operator becomes equivalent to a ref class overloading the
Object::Equals method ?

Or is it that a ref class providing an overload for the Objecct::Equals
method now has that called when a C++/CLI programmer uses the == operator ?

>> How do I compare the ref pointers themselves ( ^ ) for equality ?
>
> Object::ReferenceEquals(obj1, obj2)

Thanks ! That will always works.

However operator overloading in C++/CLI has me a bit confused. Does
C++/CLI operator overloading relate to any .NET framework virtual member
functions that can be called from other .NET languages ? if so which
operator oveloads and which member functions.

In general I have found the documentation for clr class operator
overloading in MSDN for C++/CLI to be sparse to the extreme and
explaining almost nothing. There is a single topic page which I can find
which has no explanation how C++/CLI relates to .net member functions.
Have I missed a more profuse explanation of this somewhere in the MSDN
documentation for C++/CLI ?
Marcus Heege - 25 Apr 2006 09:01 GMT
Sorry, I was quite tired when I wrote this. That's why I confused some
things here. (At least I was the only one to find it out :-).)

By default, operator == works on reference types like
Object::ReferenceEquals, however types can override operator ==.
In C++/CLI and C#, operator == NEVER maps to Object::Equals.

Here is an app that shows how it works:

// eqt.cpp
// compile with "CL /clr:safe eqt.cpp"

using namespace System;

ref class ClassWithEquals
{
 int i;
public:
 ClassWithEquals(int i) : i(i) {}

 virtual bool Equals(Object^ o) override
 {
   if (o == nullptr)
     return false;

   ClassWithEquals^ that = dynamic_cast<ClassWithEquals^>(o);
   if (that == nullptr)
     return false;

   return this->i = that->i;
 }
};

ref class ClassWithEqualityOperator
{
 int i;
public:
 ClassWithEqualityOperator(int i) : i(i) {}

 bool operator==(ClassWithEqualityOperator^ that)
 {
   if (that == (Object^)nullptr)
     return false;

   return this->i == that->i;
 }
};

int main()
{
 ClassWithEquals^ cwe1 = gcnew ClassWithEquals(1);
 ClassWithEquals^ cwe2 = gcnew ClassWithEquals(1);

 if (cwe1 == cwe2)
   Console::WriteLine("cwe1 == cwe2");

 ClassWithEqualityOperator^ cwoe1 = gcnew ClassWithEqualityOperator(1);
 ClassWithEqualityOperator^ cwoe2 = gcnew ClassWithEqualityOperator(1);

 if (cwoe1 == cwoe2)
   Console::WriteLine("cwoe1 == cwoe2");
}

If o

>> Now that operator overloading allows to ref classes to be compared for
>> equality using == syntax, how does one compare the actual ref pointers
[quoted text clipped - 13 lines]
>
> Object::ReferenceEquals(obj1, obj2)
Edward Diener - 25 Apr 2006 10:13 GMT
> Sorry, I was quite tired when I wrote this. That's why I confused some
> things here. (At least I was the only one to find it out :-).)
>
> By default, operator == works on reference types like
> Object::ReferenceEquals, however types can override operator ==.
> In C++/CLI and C#, operator == NEVER maps to Object::Equals.

Understood.

But I am gathering that if a class implements operator ==, then it
should also override Object::Equals to work in the same way in order to
be consistent.

> Here is an app that shows how it works:
>
[quoted text clipped - 51 lines]
>     Console::WriteLine("cwoe1 == cwoe2");
> }

Thanks for the example.
Marcus Heege - 25 Apr 2006 11:03 GMT
>> Sorry, I was quite tired when I wrote this. That's why I confused some
>> things here. (At least I was the only one to find it out :-).)
[quoted text clipped - 8 lines]
> also override Object::Equals to work in the same way in order to be
> consistent.

This argument sounds plausible to me. If you follow this argument, then you
should also overide Object::GetHashCode, because some container classes in
the FCL expect the following behavior:

( obj1->Equals(obj2) )    =>    ( obj1->GetHashCode() ==
obj2->GetHashCode() )

Marcus

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.