简体   繁体   中英

How should I call an overloaded operator in a base abstract class?

So I have a Base class:

class Base
{
public:
    std::ostream& operator << (std::ostream & out, const Base & base);
}

And I have defined what the operator should do:

ostream& operator << (std::ostream & out, const Base & base)
{
    return out << "output";
}

If I have a Derived class that extends Base and I want Derived to do the same thing as Base when its insertion operator is called, what is the best way to go about doing this? And by best I mean best way to not reuse code.

If you want to use a base-class method within a derived class, qualify the call:

void Derived::func()
{
   //whatever else you want to do first
   return Base::func();
}

Unless the method is private you have access to it, since Derived is a Base .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM