473,479 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

== operator

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 ) {}
}
Jul 22 '05 #1
3 1229
"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
Jul 22 '05 #2
sksjava 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

<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

--
Reverse domain name to reply.

Jul 22 '05 #3
"sksjava" <sk*****@hotmail.com> wrote in message
news:bu**********@news.lsil.com...
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. :-)
Jul 22 '05 #4

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

Similar topics

7
8015
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
1
3847
by: joesoap | last post by:
Hi can anybody please tell me what is wrong with my ostream operator??? this is the output i get using the 3 attached files. this is the output after i run assignment2 -joesoap #include...
5
3783
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
0
1814
by: Martin Magnusson | last post by:
I have defined a number of custom stream buffers with corresponding in and out streams for IO operations in my program, such as IO::output, IO::warning and IO::debug. Now, the debug stream should...
3
2926
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD...
6
4398
by: YUY0x7 | last post by:
Hi, I am having a bit of trouble with a specialization of operator<<. Here goes: class MyStream { }; template <typename T> MyStream& operator<<(MyStream& lhs, T const &)
3
18781
by: gugdias | last post by:
I'm coding a simple matrix class, which is resulting in the following error when compiling with g++ 3.4.2 (mingw-special): * declaration of `operator/' as non-function * expected `;' before '<'...
5
2265
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
8
2470
by: valerij | last post by:
Yes, hi How to write "operator +" and "operator =" functions in a class with a defined constructor? The following code demonstrates that I don't really understand how to do it... I think it has...
3
3248
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
0
7019
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7067
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6719
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...
0
6847
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4757
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2980
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...
0
2970
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1288
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 ...
1
555
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.