相关疑难解决方法(0)

从派生类对象调用基类方法

如何从派生类对象中调用派生类重写的基类方法?

class Base{
  public:
    void foo(){cout<<"base";}
};

class Derived:public Base{
  public:
    void foo(){cout<<"derived";}
}

int main(){
  Derived bar;
  //call Base::foo() from bar here?
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ inheritance

36
推荐指数
2
解决办法
7万
查看次数

死亡钻石和范围解析算子(c ++)

我有这个代码(钻石问题):

#include <iostream>
using namespace std;

struct Top
{
    void print() { cout << "Top::print()" << endl; }
};

struct Right : Top 
{
    void print() { cout << "Right::print()" << endl; }
};

struct Left : Top 
{
    void print() { cout << "Left::print()" << endl; }
};

struct Bottom: Right, Left{};

int main()
{
    Bottom b;
    b.Right::Top::print();
}
Run Code Online (Sandbox Code Playgroud)

我想print()Top课堂上打电话.

当我尝试编译它时,我得到错误:'Top' is an ambiguous base of 'Bottom'在这一行:b.Right::Top::print(); 为什么它不明确?我明确规定,我想TopRight与不从 …

c++ inheritance multiple-inheritance diamond-problem

13
推荐指数
1
解决办法
704
查看次数