Polymorphism question

I have a base class as follows:
1
2
3
4
5
6
class SYMBOL : public std::string
{  //  Bunch of SYMBOL specific attributes
    
protected:
    virtual COMPARE_CC Compare (const SYMBOL & rhs) const;
...


From SYMBOL, I have a bunch of derived classes for different types of symbols.
1
2
3
4
5
class RSETNAME : public SYMBOL
{   OWNER           m_owner; 

    COMPARE_CC Compare (const RSETNAME & rhs) const;
...


I'm getting the following error from the compiler:
rsetname.h(6,15) : warning (1319) : function "SYMBOL::Compare(const SYMBOL &) const" is hidden by "RSETNAME::Compare" -- virtual function override intended?

I understand what the error is telling me. What I'm not clear on is how to accomplish what I want to do. I'd like to be able to compare two objects derived from SYMBOL but which may or may not be RSETNAMES. If they are both RSETNAMEs, then use the RSETNAME::Compare function.

Any ideas? Please limit suggestions to C++03. I'm stuck on a C++03 platform and can not change that.
You do not want to use a virtual function for this. You need to create a custom override (as you are doing) in each class. Eliminate the "virtual" from the base class. It is not a virtual function.

As an aside, inheriting from std::string like this is ill-advised. The STL containers (including std::string) were not designed to be base classes and inherited from. They are "final". You can tell this because the destructors are not declared virtual.

Here's a good discussion of the topic: http://stackoverflow.com/questions/9135501/why-doesn-t-stdstring-have-a-virtual-destructor
Eliminate the "virtual" from the base class.

I had it that way, but that wasn't accomplishing what I wanted.

What I want to do is compare two symbols and invoke the appropriate derived Compare function. I don't necessarily know the types of the symbols when I'm comparing them (nor do I want to). I was looking at polymorphism to do that for me.

The more I think about this, the more I think this is problematic. What if the left and right side are different derived types?

Thanks for the pointer on deriving from std::string. I wasn't aware the destructor wasn't virtual.

Only like things are comparable. Sounds like you have a bowl of fruit (base class) and you are comparing apples and oranges. If you need a different comparator for each type pair, you have a combinatorial problem that is orthogonal to your hierarchy.
I like your bowl of fruit metaphor. :) I think I've found a solution though.

SYMBOL has a get_Name() function that returns a string.
1
2
3
4
5
6
7
8
9
class SYMBOL
{  string m_name;
...
protected:
    COMPARE_CC Compare (const SYMBOL & rhs) const;  // non-virtual
public: 
   virtual const string & get_name () const
   {  return m_name;
   }

Now in RSETNAME, I can overload get_name to return m_name concatenated with m_owner since I want to qualify RSETNAME by the owner attribute.

SYMBOL's Compare function now becomes:
1
2
3
4
5
6
7
COMPARE_CC SYMBOL::Compare (const SYMBOL & rhs) const
{   if (get_name() < rhs.get_name())
        return CC_LT;
    if (get_name() > rhs.get_name())
        return CC_GT;
    return CC_EQ;
}

This will get and compare the overloaded names, which serves my purpose.

I'm marking this as solved.

Thanks PanGalactic.

Last edited on
Topic archived. No new replies allowed.