c++重载和继承,调用new方法

C++ overloading and inheritance, call new method

本文关键字:调用 new 方法 继承 重载 c++      更新时间:2023-10-16

我有一个类a:

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

和B类,B类派生自A:

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

现在当我这样做的时候:

B b;
b.do_something();
调用A的do_end()方法。我如何使用的do_something()的A,但有它调用方法do_end()的B?

有办法吗?

谢谢

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

  virtual SomeType do_end(){
    finish...
  }