5

从这个问题开始:

考虑到这个简化的代码:

#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.
}

我有两个疑问。1. 我知道如果我从with 中“导入”一个method名称,我可以解决这个错误Abstract

using Abstract::method;

但随后我导入了该名称的所有重载。 是否可以仅导入给定的重载? (假设Abstract有不止一个重载,例如:)

virtual void method(int a) = 0;
virtual void method(std::string s) = 0;
  1. 是否可以一次导入所有 (public|protected|all) 名称Abstract而不一一列出?

(现在假设除了methodAbstract还有:)

virtual void foo() = 0;
4

0 回答 0