473,804 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(..initiali zed somehow);
SomeRefObject ^ obj2(..initiali zed 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 4510
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************ *******@tropics oft.com> wrote in message
news:et******** ******@TK2MSFTN GP04.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(..initiali zed somehow);
SomeRefObject ^ obj2(..initiali zed 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::Referen ceEquals(obj1, obj2)

Apr 24 '06 #3
Marcus Heege wrote:
"Edward Diener" <ed************ *******@tropics oft.com> wrote in message
news:et******** ******@TK2MSFTN GP04.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(..initiali zed somehow);
SomeRefObject ^ obj2(..initiali zed 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::Referen ceEquals(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::Referen ceEquals, 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<Cl assWithEquals^> (o);
if (that == nullptr)
return false;

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

ref class ClassWithEquali tyOperator
{
int i;
public:
ClassWithEquali tyOperator(int i) : i(i) {}

bool operator==(Clas sWithEqualityOp erator^ that)
{
if (that == (Object^)nullpt r)
return false;

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

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

if (cwe1 == cwe2)
Console::WriteL ine("cwe1 == cwe2");

ClassWithEquali tyOperator^ cwoe1 = gcnew ClassWithEquali tyOperator(1);
ClassWithEquali tyOperator^ cwoe2 = gcnew ClassWithEquali tyOperator(1);

if (cwoe1 == cwoe2)
Console::WriteL ine("cwoe1 == cwoe2");
}

If o

"Marcus Heege" <NO****@heege.n et> wrote in message
news:u%******** ********@TK2MSF TNGP02.phx.gbl. ..
"Edward Diener" <ed************ *******@tropics oft.com> wrote in message
news:et******** ******@TK2MSFTN GP04.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(..initiali zed somehow);
SomeRefObject ^ obj2(..initiali zed 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::Referen ceEquals(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::Referen ceEquals, 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<Cl assWithEquals^> (o);
if (that == nullptr)
return false;

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

ref class ClassWithEquali tyOperator
{
int i;
public:
ClassWithEquali tyOperator(int i) : i(i) {}

bool operator==(Clas sWithEqualityOp erator^ that)
{
if (that == (Object^)nullpt r)
return false;

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

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

if (cwe1 == cwe2)
Console::WriteL ine("cwe1 == cwe2");

ClassWithEquali tyOperator^ cwoe1 = gcnew ClassWithEquali tyOperator(1);
ClassWithEquali tyOperator^ cwoe2 = gcnew ClassWithEquali tyOperator(1);

if (cwoe1 == cwoe2)
Console::WriteL ine("cwoe1 == cwoe2");
}


Thanks for the example.
Apr 25 '06 #6
"Edward Diener" <ed************ *******@tropics oft.com> wrote in message
news:e9******** ******@TK2MSFTN GP02.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::Referen ceEquals, 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::GetHash Code, 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
1294
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
2659
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 = Color.Empty then..... or if mycolor is Color.Empty then .......
19
8754
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 there another command I need to through in there or should I do it a completely different way? Any suggestions would be most appreciated.
12
26013
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
2171
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 mmapped to a single page. I want to determine if the second is on the page. I'd like to do: #include "platform_appropriate_definition_of_PAGESIZE.h" int compare1(const char *a, const char *b)
12
6863
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) (fabs((a)-(b))<=(DBL_EPSILON)*fabs((a))) Do the same issues involved in comparing 2 fp types for equality apply to comparing a float to zero? E.g. is if(x == 0.0) considered harmful?
25
13064
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 *p, void *q) that will take any two pointers and decide whether they can be compared or not. I really can't think how to do this - any suggestions?
27
3872
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 fails if comparing doubles of different magnitude since it's hard or not possible to find a suitable threshold
10
5033
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++ Primer - 4/e * * exercise 9.20 * STATEMENT: * Write a program to to compare whether a vector<intcontains the
0
9593
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9169
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.