473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Operator overloading for a hierarchy

Hi all,

I could have sworn the c++ faq had the answer for this question but I
can't seem to find it, so here goes:

Suppose you have a single-rooted class hierarchy. Call the root class
Object. How can we provide operator overloading for the whole
hierarchy? e.g. provide == for the whole hierarchy?

Even more specifically: suppose we define

bool operator==(const Object* lhs, const Object* rhs) {
return lhs == rhs;
}

but we want to allow subclasses to override this behaviour by
overrididing some (virtual) method(s), so that even if you have only a
pointer to the base class (e.g. Object) the right comparison is made.
What is the best way to organize things?

TIA, best regards,
G. Rodrigues
Dec 11 '06 #1
2 1699

Gonçalo Rodrigues wrote:
Hi all,

I could have sworn the c++ faq had the answer for this question but I
can't seem to find it, so here goes:

Suppose you have a single-rooted class hierarchy. Call the root class
Object. How can we provide operator overloading for the whole
hierarchy? e.g. provide == for the whole hierarchy?

Even more specifically: suppose we define

bool operator==(const Object* lhs, const Object* rhs) {
return lhs == rhs;
}

but we want to allow subclasses to override this behaviour by
overrididing some (virtual) method(s), so that even if you have only a
pointer to the base class (e.g. Object) the right comparison is made.
What is the best way to organize things?
bool operator==(Object const& lhs, Object const& rhs) // use
references, not pointers
{
return lhs.equals(rhs); // call virtual equals function
}

The problem you may run into, and the answer depends highly on your
needs, is what if lhs and rhs are different eventual types.

Dec 11 '06 #2
Gonçalo Rodrigues wrote:
Hi all,

I could have sworn the c++ faq had the answer for this question but I
can't seem to find it, so here goes:

Suppose you have a single-rooted class hierarchy. Call the root class
Object. How can we provide operator overloading for the whole
hierarchy? e.g. provide == for the whole hierarchy?

Even more specifically: suppose we define

bool operator==(const Object* lhs, const Object* rhs) {
Huh? Pointers??
return lhs == rhs;
}

but we want to allow subclasses to override this behaviour by
overrididing some (virtual) method(s), so that even if you have only a
pointer to the base class (e.g. Object) the right comparison is made.
What is the best way to organize things?
class Object {
virtual bool operator ==(const Object& rhs) const;
};

class SomeOtherClass : public Object {
typedef Object base; // or whatever
virtual bool operator ==(const Object& rhs) const {
if (SomeOtherClass const *p = /* declare/define/init */
dynamic_cast<SomeOtherClass const*>(&rhs)) {
// do the comparison between '*this' and '*p'
}
else { // fall back onto Object
return this->base::operator==(rhs);
}
}
};

If you have a more sophisticated comparison, look up double dispatch.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 11 '06 #3

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

Similar topics

16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
13
by: Ankit | last post by:
Hello, I have an old VC++ project code base which I am trying to build and use. This uses an ostream object. Now in my project, I have overloaded the leftshift operator ( << ), basically being...
3
by: karthik | last post by:
The * operator behaves in 2 different ways. It is used as the value at address operator as well as the multiplication operator. Does this mean * is overloaded in c?
5
by: noone | last post by:
hi. I don't use exceptions much in the embedded world, but for my plugin interface to a hardware MPEG encoder I'd like to, since there are so many places that the crummy kernel driver can do bad...
3
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,...
14
by: Hunk | last post by:
Hi I ws wondering if there is a way to implement operator+ in case of virtual classes. Here's the problem. I have to have a base string class from which two classes (normal char string and a...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
3
by: dizzy | last post by:
Hi I wonder if this code is standard conformant and should work on all conformant implementations (for some type T): 1: void* mem = ::operator new(sizeof(T)); 2: T* p = new(mem) T(args...);...
6
by: Rob McDonald | last post by:
I would like to force all the classes in my hierarchy to implement the << operator for testing purposes. My base class is a pure virtual class. I started out by working with operator...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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,...

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.