Given this code:
class base {
public:
string foo() const; // Want this to be visible in 'derived' class.
}
class derived : public base {
public:
virtual int foo(int) const; // Causes base class foo() to be hidden.
}
How can I make base::foo() visible to derived without replicating it with a dummy method overloading that calls the base class? Does using
do the trick, if so, where does it go, is it like this?
class derived : public base {
public:
virtual int foo(int) const;
using base::foo;
}
Sorry for the short answer, but yes. It's exactly like this and does what you want:
class derived : public base {
public:
virtual int foo(int) const;
using base::foo;
};
Also note that you can access the base even without using
:
derived x;
string str = x.base::foo(); // works without using
The using directive will make selected methods from base visible to the scope of derived. But from a design point of view this is not desirable, since you're hiding names to the scope of derived while using a public interface. (Doing this with a private interface makes more sense)
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.