"sksjava" <sk*****@hotmail.com> wrote...
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