CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2009
    Posts
    40

    [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?

  2. #2
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Inheritance, polymorphism, and comparison operators in a binary search tree

    [ Moved thread ]

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Belo Horizonte, Brazil
    Posts
    405

    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
  •  





Click Here to Expand Forum to Full Width

Featured