从这个问题开始:
考虑到这个简化的代码:
#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;
- 是否可以一次导入所有 (public|protected|all) 名称
Abstract
而不一一列出?
(现在假设除了method
,Abstract
还有:)
virtual void foo() = 0;