-
February 19th, 2011, 09:40 PM
#1
[RESOLVED] Inheritance, polymorphism, and comparison operators in a binary search tree
I have a binary search tree that the nodes point to object A. There is a three stage inheritance of objects: B is a subclass for A and C is a subclass for B. so, A is the top level superclass for the derived class C. In the binary search tree, to compare objects, the class uses operator<, operator>, and operator==. The binary search tree is only aware of the A level, but I somehow need the comparison to trickle down to class C as A and B are pure virtual. Can this be done?
-
February 20th, 2011, 02:17 AM
#2
Re: Inheritance, polymorphism, and comparison operators in a binary search tree
-
February 20th, 2011, 07:39 AM
#3
Re: Inheritance, polymorphism, and comparison operators in a binary search tree
Overloaded operators cannot be made virtual, so you cannot use them directly. However, the real problem is not about syntax, but about how to make such a construct work correctly in all cases. Also see my reply in this thread: http://www.codeguru.com/forum/showthread.php?t=508895
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
February 20th, 2011, 09:56 AM
#4
Re: Inheritance, polymorphism, and comparison operators in a binary search tree
As D_Drmmr pointed the problem is a bit deeper than simply making functions virtual. It's hard to come up with a nice design that would work under all circumstances. You can find a lot of discussion concerning operators and polymorphism (with casting and different approaches). For example, this one and this other one.
In cases like this I usually create an artificial typeId() member function that I could use to query and identify that actual instance type. Based on that I am able to apply the appropriate logic.
I have also done a slight variation of the approach mentioned in the thread pointed by D_Drmmr. However, it has a drawback which is that it requires the base class to be aware of its derived classes. It's not scalable and more subjected to maintenance issues. Still, here's the idea.
Code:
struct OneDerived;
struct OtherDerived;
struct Base
{
virtual bool equals(const Base *base) const = 0;
virtual bool equals(const OneDerived* one) const { return false; }
virtual bool equals(const OtherDerived* other) const { return false; }
};
struct OneDerived
{
virtual bool equals(const Base *base) const
{ return base->equals(this); }
virtual bool equals(const OneDerived *one) const
{ /* Actual code the compares for OneDerived */ }
};
struct OtherDerived
{
virtual bool equals(const Base *base) const
{ return base->equals(this); }
virtual bool equals(const OtherDerived *other) const
{ /* Actual code the compares for OtherDerived */ }
};
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|