开发者

How to expose hidden overloading from base class?

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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜