== operator

S

sksjava

I have overloaded the "==" operator for a class. But now I want to only
compare the addresses of the objects, is there is a c++ mechanism to do this
without casting?

Ex:- (not compileable)
void whatever(Something* one, Something* two) // "==" is overloaded for
class Something
{
// want to compare one's and two's addresses
if ( one == two ) {} // don't want to invoke the "==" operator

// only solution that I can think of
void* onev = reinterpret_cast<void*>(one);
void* twov = reinterpret_cast<void*>(two);
if ( onev == twov ) {}
}
 
V

Victor Bazarov

sksjava said:
I have overloaded the "==" operator for a class. But now I want to only
compare the addresses of the objects, is there is a c++ mechanism to do this
without casting?

Ex:- (not compileable)
void whatever(Something* one, Something* two) // "==" is overloaded for
class Something
{
// want to compare one's and two's addresses
if ( one == two ) {} // don't want to invoke the "==" operator

// only solution that I can think of
void* onev = reinterpret_cast<void*>(one);
void* twov = reinterpret_cast<void*>(two);
if ( onev == twov ) {}
}

(a) You cannot change the default behaviour of comparing pointers (you
can call them addresses, but they are still pointers, a built-in
C++ type) because you cannot change the behaviour of the operators
for built-in types. E.G. you cannot overload operator+(int,int).

(b) Even though you cannot change it, why not simply rely on comparing
pointers provided to you by C++ already? What do you find not to
your liking in it?

(c) Any pointer-to-object can be converted to a pointer-to-void without
a cast (IOW, implicitly), so you can drop the casts from your code.

V
 
A

Adam Fineman

sksjava said:
I have overloaded the "==" operator for a class. But now I want to only
compare the addresses of the objects, is there is a c++ mechanism to do this
without casting?

Ex:- (not compileable)
void whatever(Something* one, Something* two) // "==" is overloaded for
class Something
{
// want to compare one's and two's addresses
if ( one == two ) {} // don't want to invoke the "==" operator
<snip>

You don't have to do anything. When you compare two pointers, you are
already comparing addresses, not what they are pointing to.

Write yourself a small test program and prove this to yourself.

- Adam
 
S

sksjava

sksjava said:
I have overloaded the "==" operator for a class. But now I want to only
compare the addresses of the objects, is there is a c++ mechanism to do this
without casting?

Ex:- (not compileable)
void whatever(Something* one, Something* two) // "==" is overloaded for
class Something
{
// want to compare one's and two's addresses
if ( one == two ) {} // don't want to invoke the "==" operator

// only solution that I can think of
void* onev = reinterpret_cast<void*>(one);
void* twov = reinterpret_cast<void*>(two);
if ( onev == twov ) {}
}

My apologies. I am not all there most of the time and even worse today. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,416
Latest member
LionelQ387

Latest Threads

Top