从这个问题开始:
- 从纯虚类(A)派生的指针无法访问纯类(B)的重载方法 https://stackoverflow.com/questions/59756738/pointer-derived-from-pure-virtual-classa-cant-access-overload-method-from-the
考虑这个简化的代码:
#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 https://godbolt.org/z/iAyqA2)
我有两个疑问。
1. 我知道如果我“导入”a 就可以解决该错误method
名字来自Abstract
with
using Abstract::method;
但随后我导入该名称的所有重载。是否可以仅导入给定的重载?(认为Abstract
有不止一个重载,例如:)
virtual void method(int a) = 0;
virtual void method(std::string s) = 0;
- 是否可以导入所有(公共|受保护|所有)名称
Abstract
立即不将它们一一列出?
(现在假设除了method
, Abstract
也有:)
virtual void foo() = 0;
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)