简体   繁体   English

从Abstract Base Class访问另一个Base Classes成员

[英]Access another Base Classes member from Abstract Base Class

I wonder if it is possible to declare a pure virtual function in class AbstractBase and make a Base classes member visible in Derived so it will use the member of Base and not look for a implementation in Derived . 我想知道是否有可能在类AbstractBase声明一个纯虚函数并使DerivedBase类成员可见,因此它将使用Base的成员而不在Derived查找实现。 So far, i tried making Base 's member visual by trying to use using but it won't compile since the look up, in this case, seems to ignore using. 到目前为止,我尝试通过尝试使用using来使Base的成员可视化但是它不会编译,因为查找,在这种情况下,似乎忽略使用。 Is this possible at all? 这有可能吗? Here is my code: 这是我的代码:

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

Use :: operator: 使用::运算符:

class Derived : public Base {

public:
    Derived(){}
private:
    void f(){ Base::f() }  
};

Also, you don't need to inherit from AbstractBase . 此外,您不需要从AbstractBase继承。

It looks to me that you would like f() to be pure-virtual but provide default implementation. 在我看来,您希望f()是纯虚拟的,但提供默认实现。 In this case, it can be achieved this way: 在这种情况下,可以通过这种方式实现:

#include <iostream>

using namespace std;

struct AbstractBaseWithDefaultF 
{
    virtual ~AbstractBaseWithDefaultF() = default;
    virtual void f() = 0;
};
void AbstractBaseWithDefaultF::f()
{
    cout << "called AbstractBaseWithDefaultF's f()" << endl;
}

struct Derived : AbstractBaseWithDefaultF
{
    void f() override 
    {
        AbstractBaseWithDefaultF::f();
        cout << "called Derived's f()" << endl;
    } 
};

int main()
{
    Derived d;
    d.f();
}

Output: 输出:

called AbstractBaseWithDefaultF's f()
called Derived's f()

Here's a live Wandbox example . 这是一个实时的Wandbox示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 多态C ++,尝试访问从抽象基类派生的类的成员函数 - polymorphism c++, trying to access member functions of classes that are derived from abstract base class 抽象基类成员变量 - Abstract base class member variables 基类中的抽象基成员变量 - Abstract base member variable in base class 从派生类调用的抽象基类成员函数 - Abstract base class member function called from derived class 两个抽象类,派生自相同的基础 class。 如何访问从一个抽象 class 到另一个的指针 - Two abstract classes, deriving from the same base class. How can I access a pointer from one abstract class to the other 从抽象基类成员函数调用纯虚函数? - Calling Pure Virtual Function From Abstract Base Class Member Function? 仅授予某些派生类访问基类中的成员函数的权限 - Give only some derived classes access to member functions from base class C ++从基类组成两个类来访问私有成员 - C++ access private member in composition of two classes from base class 从派生类访问成员函数的基类指针向量? 正确的方法? - Vector of base class pointers to access member functions from derived classes? The correct way? 我可以覆盖抽象基类的成员吗? - Can I overide a member of an abstract base class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM