我在想,是否可以在類AbstractBase
中聲明純虛函數,並在Derived
中使Base
類成員可見,因此它將使用Base
的成員,而不是查找實現在Derived
。到目前爲止,我嘗試使用using
來嘗試使Base
的成員可視化,但它不會編譯,因爲在這種情況下,查找似乎忽略了使用。這可能嗎?這裏是我的代碼:訪問抽象基類中的另一個基類成員
#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;
}
就這麼簡單。這是一個簡化的案例。當再次使用'Derived'作爲'AbstractBase'時,這會很有用。謝謝! – tobilocker
只是爲了清楚'::'不是一個操作符。 –
@JonathanMee那是什麼?對我來說,它也被稱爲「命名空間運算符」。 – tobilocker