下面的代码有错误:最后一行应该是
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