Operator overloading for a hierarchy

G

Gonçalo Rodrigues

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
 
N

Noah Roberts

Gonçalo Rodrigues said:
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.
 
V

Victor Bazarov

Gonçalo Rodrigues said:
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::eek:perator==(rhs);
}
}
};

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

V
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top