473,320 Members | 1,825 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

C++/CLI comparing two clr poiinters for equality

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 ?
Apr 24 '06 #1
6 4468
Edward Diener wrote:
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/lib...ct.equals.aspx

Tom
Apr 24 '06 #2
"Edward Diener" <ed*******************@tropicsoft.com> wrote in message
news:et**************@TK2MSFTNGP04.phx.gbl...
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.
If SomeRefObject overwrites Equals, yes, otherwise no
How do I compare the ref pointers themselves ( ^ ) for equality ?


Object::ReferenceEquals(obj1, obj2)

Apr 24 '06 #3
Marcus Heege wrote:
"Edward Diener" <ed*******************@tropicsoft.com> wrote in message
news:et**************@TK2MSFTNGP04.phx.gbl...
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.
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 ?
Apr 24 '06 #4
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

"Marcus Heege" <NO****@heege.net> wrote in message
news:u%****************@TK2MSFTNGP02.phx.gbl...
"Edward Diener" <ed*******************@tropicsoft.com> wrote in message
news:et**************@TK2MSFTNGP04.phx.gbl...
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.


If SomeRefObject overwrites Equals, yes, otherwise no
How do I compare the ref pointers themselves ( ^ ) for equality ?


Object::ReferenceEquals(obj1, obj2)

Apr 25 '06 #5
Marcus Heege wrote:
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:

// 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");
}


Thanks for the example.
Apr 25 '06 #6
"Edward Diener" <ed*******************@tropicsoft.com> wrote in message
news:e9**************@TK2MSFTNGP02.phx.gbl...
Marcus Heege wrote:
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.


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
Apr 25 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Peng Yu | last post by:
I'm trying to define a list of integer poiinters. But the following thing doesn't work. Do you know what's wrong with it? list<int*> L; Thanks! Peng
19
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
19
by: nitro_punk85 | last post by:
I'm working on a project for my c++ class and I am having trouble comparing one string to two others using the or operator. It looks something like this: if(answer3 == answer1 || answer2) Is...
12
by: barcaroller | last post by:
Is it legal to compare the contents of two multi-field variables (of the same struct) using "==" and "!="? struct { int a; int b; } x,y; ...
20
by: Bill Pursell | last post by:
This question involves code relying on mmap, and thus is not maximally portable. Undoubtedly, many will complain that my question is not topical... I have two pointers, the first of which is...
12
by: John Smith | last post by:
This code for the comparison of fp types is taken from the C FAQ. Any problems using it in a macro? /* compare 2 doubles for equality */ #define DBL_ISEQUAL(a,b)...
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
27
by: Thomas Kowalski | last post by:
Hi everyone, To determine equality of two doubles a and b the following is often done: bool isEqual ( double a, double b ) { return ( fabs (a-b) < THRESHOLD ); } But this a approach usually...
10
by: arnuld | last post by:
It is quite an ugly hack but it is all I am able to come up with for now :-( and it does the requires work. I want to improve the program, I know you people have much better ideas ;-) /* C++...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.