我想知道是否有可能在类中声明纯虚函数AbstractBase
并使Base
类的成员可见,Derived
以便它使用的成员Base
而不在中寻找实现Derived
。到目前为止,我尝试Base
通过尝试使用using
使其成员可见,但由于在这种情况下查找似乎忽略了使用,因此它无法编译。这有可能吗?这是我的代码:
#include <iostream>
using namespace std;
class AbstractBase {
public:
AbstractBase(){}
virtual ~AbstractBase(){}
protected:
virtual void f() = 0;
};
class Base {
public:
Base(){}
protected:
void f() {cout << "called Base's f()" << endl;}
};
class Derived : public Base, public AbstractBase {
public:
Derived(){}
//using Base::f; /*this won't compile*/
private:
void f(){} /*Access Base's f() here rather than implement*/
};
int main()
{
Derived d;
}
使用::
运算符:
class Derived : public Base {
public:
Derived(){}
private:
void f(){ Base::f() }
};
另外,您无需继承AbstractBase
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句