473,467 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Multiple Overloaded ==

Is it possible to have multiple overloaded == operators for a given
class?

So that I can say two objects are == if they share the same name but
also two objects are equal if they share the same unique int code?

To me it makes sense to allow multiple ones but I have never bumped
into it before.

Aug 16 '07 #1
3 1154
Travis wrote:
Is it possible to have multiple overloaded == operators for a given
class?

So that I can say two objects are == if they share the same name but
also two objects are equal if they share the same unique int code?

To me it makes sense to allow multiple ones but I have never bumped
into it before.
Not exactly. You can only overload functions based upon the types of
their arguments. In this case you don't need that. The way you do this
is by using an || in your == operator. Something like:

class A
{
public:
bool operator==(const A & a) const
{
return m_Id == a.m_Id || m_Name == a.m_Name;
}
private:
int m_Id;
std::string m_Name;
};

Hope that helps.

joe

Aug 16 '07 #2
On Aug 16, 10:41 am, Joe Greer <jgr...@doubletake.comwrote:
Travis wrote:
Is it possible to have multiple overloaded == operators for a given
class?
So that I can say two objects are == if they share the same name but
also two objects are equal if they share the same unique int code?
To me it makes sense to allow multiple ones but I have never bumped
into it before.

Not exactly. You can only overload functions based upon the types of
their arguments. In this case you don't need that. The way you do this
is by using an || in your == operator. Something like:

class A
{
public:
bool operator==(const A & a) const
{
return m_Id == a.m_Id || m_Name == a.m_Name;
}
private:
int m_Id;
std::string m_Name;

};

Hope that helps.

joe
Thanks for the help. Maybe I am going about it the wrong way. So
basically what I'm wanting to do, is get a node in the tree based on
some criteria. I wanted tree to remain as reusable as possible so I
would like to avoid modifying it (it's a template, etc.).

So the tree is full of myNodeType I would like to be able to say

myNodeType *foundNode = MenuTreeObj->findNode(ID); // find based on ID

- or -

myNodeType *foundNode = MenuTreeObj->findNode(unique_name); // find
based on name

Is that even practical? What I had been doing in the past (since the
== makes determination based on name).

myNodeType *tmp = new myNodeType("unique_name_to_be_found");
myNodeType *foundNode = MenuTreeObj->findNode(tmp);

Aug 16 '07 #3
Travis wrote:
On Aug 16, 10:41 am, Joe Greer <jgr...@doubletake.comwrote:
>Travis wrote:
>>Is it possible to have multiple overloaded == operators for a given
class?
[snip]
Thanks for the help. Maybe I am going about it the wrong way. So
basically what I'm wanting to do, is get a node in the tree based on
some criteria. I wanted tree to remain as reusable as possible so I
would like to avoid modifying it (it's a template, etc.).
DO NOT REWRITE ANOTHER TREE.
There are many that exist, especially std::map. It has already been
tested and comes with C++ (if not, you can easily download it).

The STL containers are written to be as reusable as possible.
Also read up on iterators.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
Aug 17 '07 #4

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

Similar topics

12
by: Philip Smith | last post by:
Call this a C++ programmers hang-up if you like. I don't seem to be able to define multiple versions of __init__ in my matrix class (ie to initialise either from a list of values or from 2...
8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a...
5
by: IvDČ | last post by:
During a project I ran into trouble when using multiple inheritance. I was able to resolve the problem, but was unable to really understand the reasons for the error. Here is a short example of...
44
by: bahadir.balban | last post by:
Hi, What's the best way to implement an overloaded function in C? For instance if you want to have generic print function for various structures, my implementation would be with a case...
3
by: cybertof | last post by:
Hello, Is it possible in C# to have 2 overloaded functions with - same names - same parameters - different return type values If no, is it possible in another language ?
3
by: computerwolf8 | last post by:
I have a file where I know the lines go as follows: string long string int int string double
5
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...
4
by: chandu | last post by:
Hello, I had one doubt,please clarify it. Main is the entry point for the application. if i had multiple overloaded main methods in my application. how the compiler recognizes the original...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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
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...
0
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
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
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 ...

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.