How to make all hidden names from a base class accessible in derived one?

不打扰是莪最后的温柔 提交于 2020-01-22 20:39:33

问题


Starting from this question:

  • Pointer derived from pure virtual class(A) can't access overload method from the pure class (B)

And considering this simplified code:

#include <string>
#include <iostream>

class Abstract
{
public:
    virtual void method(int a)
    {
        std::cout << __PRETTY_FUNCTION__ << "a: " << a << std::endl;
    }
};

class Concrete : public Abstract
{
public:
    void method(char c, std::string s)
    {
        std::cout << __PRETTY_FUNCTION__ << "c: " << c << "; s: " << s << std::endl;
    }
};

int main()
{
    Concrete c;
    c.method(42);    // error: no matching function for call to 'Concrete::method(int)'
    c.method('a', std::string("S1_1"));

    Abstract *ptr = &c;
    ptr->method(13);
    //ptr->method('b', std::string("string2"));    <- FAIL, this is not declared in Abstract.
}
  • (Try it)

I got two doubts. 1. I know I can solve the error if I "import" a method name from Abstract with

using Abstract::method;

but then I import all overloads of that name. Would it be possible to import only a given overload? (Suppose Abstract has more than one overload like:)

virtual void method(int a) = 0;
virtual void method(std::string s) = 0;
  1. Is it possible to import all (public|protected|all) names from Abstract at once without listing them one-by-one?

(Now suppose that in addition to method, Abstract also has:)

virtual void foo() = 0;

来源:https://stackoverflow.com/questions/59758453/how-to-make-all-hidden-names-from-a-base-class-accessible-in-derived-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!