Reputation: 613
I know that a derived class "is a" base class, therefore you can always pass a derived object to a base member function. Now, I was wondering about the reverse situation specifically with comparing operators (base class is not abstract and has objects).
Lets say I have:
class Base:
{
public:
Base(int m1, string m2);
virtual ~Base();
int GetM1()const;
string GetM2()const;
virtual bool operator<(const Base& base)const;
private:
int m1;
string m2;
};
And I want to do something like this:
class Derived: public Base
{
public:
Derived(string member);
~Derived();
virtual bool operator<(const Base& base)const; // is this possible(without cast)???
};
Thanks
Upvotes: 1
Views: 45
Reputation: 780655
Yes, it's possible. The Derived
opererator will be used in code like this:
Base b;
Derived d;
if (d < b) {
...
}
You could also have some other class derived from Base
, such as Derived1
, and it will be used:
Derived1 d1;
Derived d;
if (d < d1) {
...
}
Upvotes: 2