Multiple Overloaded ==

T

Travis

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.
 
J

Joe Greer

Travis said:
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
 
T

Travis

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);
 
J

Joe Greer

Travis said:
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);

That changes the complexion of things a bit. You require a strict
ordering for a tree, so you can't use an either-or overload like I first
suggested.

It sounds like what you want to do requires two trees each one using a
different method for comparing. A simple overload of == won't do since
that code has no way of knowing which key you want to use when. The
choices would be to create two free functions that compare your objects
using the appropriate fields or you can create two member functions that
compare the appropriate fields.

As an example:

bool Cmpid(myNodeType * pA1, myNodeType * pA2)
{
return pA1->id == pA2->id;
}

-or-

class myNodeType
{
bool Cmpid(myNodeType *pA)
{
return id == pA->id;
}
};

And much the same for the name field.

If you don't have to roll your own structures, I would look up
boost::multiindex from www.boost.org as that is exactly what that class
is for... A container with multiple indexes. If you can't use 3rd
party software, but can use stl classes, then I would replace your trees
with std::map or std::set. maps are much easier in this case since your
keys are a subset of your class type. Using stl you would have two maps
one for the id and one for the name. These would then contain a pointer
to the actual object you are trying to find.

Another possibility might be (depending upon what your id is for....) to
store pointers to your objects in a vector and use the index as the id
of the object. Then create a map as an index to find the object by
name. Then looking up by index becomes simply verifying the index is in
range, and looking in that slot to see if an object has been assigned.
If it is, you return it. Finding by name would involve doing a lookup
in the map by name.

There are lots of ways to organize these things for proper memory
management, but whatever you do, make sure that the indexes and the
actual existence of the objects are kept in sync or you will get some
really wierd errors. In fact, I would suggest that you wrap all this
indexing logic along with the ownership of the objects being indexed
into a class of its own. That way you can make sure that you don't
accidentally forget a step later on when you have forgotten the
complexities of this stuff.

joe
 
A

Alexander Dünisch

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);

To find your Node in the Tree, you have to apply the exact same
criterium as used when inserting the Node into the Tree.
This can be either name or id or a combination of the two.

If you do your inserts by name and then search by id, your findNode()
function may not find anything, even if the respective node is in the
tree.

In other words, to make use of the O(lg n) of a (balanced) tree,
you have to apply the same criterium when searching as you did when
inserting.
Of course you can still search all the nodes in the tree for any
criterium, but this will cost you O(n).
Its only a matter of implementation.

Of course, for a class called A, you cannot have two overloaded
bool operator==(const A&) const;

Basically, overloading the == operator is used to introduce a concept
of equality (as opposed to object identity) for a specific type.
Needless to say, this equality comparison should be based on a unique
criterium.
In order to test for object identity, you can still compare memory
addresses.


regards,
Alex
 
T

Thomas Matthews

Travis said:
[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
 

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

Forum statistics

Threads
474,202
Messages
2,571,055
Members
47,658
Latest member
jaguar32

Latest Threads

Top