下面的代碼有錯誤:最後一行應該是
bp->g();
的問題是,如果我註釋掉該行,bp->f()
實際上調用派生版本,所以我想作爲類派生的編譯器對待BP,那麼爲什麼當調用g,編譯器將bp
視爲基址指針。重寫?上溯造型?
謝謝!
#include <iostream>
using namespace std;
class Base {
public:
virtual void f() const { cout << "Base::f()\n"<< endl; }
virtual void g() const { cout << "Base::g()\n"<< endl; }
};
class Derived : public Base {
public:
void f() const {cout << "Derived::f()" << endl; }
void g(int) const {cout << "Derived::g()" << endl; }
};
int main() {
Base* bp = new Derived;
bp->f();
bp->g(1);
}
在Drived類'void g(int ??)'變量名缺失。 – 2013-04-07 15:59:40
@GrijeshChauhan該變量未使用,因此不需要名稱。 – juanchopanza 2013-04-07 16:00:27
@juanchopanza好的我剛剛注意到 – 2013-04-07 16:00:56