【问题标题】:C++ overloading and inheritance, call new methodC++重载和继承,调用新方法
【发布时间】:2014-08-06 21:49:18
【问题描述】:

我有一个 A 类:

class A {
  public:
  do_end(){
    finish...
  }
  do_something(){
    stuff is done...
    do_end();
  }
}

和派生自A的B类:

class B :public A {
  public:
  do_end(){
    finish differently...
  }
}

现在当我这样做时:

B b;
b.do_something();

调用 A 的 do_end() 方法。 如何使用 A 的 do_something() 却调用 B 的 do_end() 方法?

有办法吗?

谢谢

【问题讨论】:

  • 请贴出真实代码。

标签: c++ inheritance


【解决方案1】:

将 A 类中的函数 do_end 定义为具有函数说明符 virtual

  virtual SomeType do_end(){
    finish...
  }

【讨论】:

    猜你喜欢
    • 2013-04-25
    • 2017-01-07
    • 2011-02-27
    • 2015-08-04
    • 2014-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    相关资源
    最近更新 更多