다음 코드에서 pBase
에서 Base::g()
에 어떻게 액세스합니까? Derived::g()
완전히 같은 Derived::f()
이다, 또한C++에서 재정의 부모 가상 메서드 액세스
Derived::f()
Derived::g()
Base::h()
Derived::f()
Derived::g()
Derived::h()
:
#include <iostream>
using namespace std;
class Base
{
public:
virtual void f(){ cout << "Base::f()" << endl; }
virtual void g(){ cout << "Base::g()" << endl; }
void h(){ cout << "Base::h()" << endl; }
};
class Derived : public Base
{
public:
void f(){ cout << "Derived::f()" << endl; }
virtual void g(){ cout << "Derived::g()" << endl; }
void h(){ cout << "Derived::h()" << endl; }
};
int main()
{
Base *pBase = new Derived;
pBase->f();
pBase->g();
pBase->h();
Derived *pDerived = new Derived;
pDerived->f();
pDerived->g();
pDerived->h();
return 0;
}
출력은 (그리고 여전히 아래처럼 일 "pBase->g();
"를 얻을)? (예. 자동 virtual
로 정의?)
귀하의 코드는 (표시 한대로)'f' 함수를 호출하지 않으므로 요청 된 출력이 코드와 일치하지 않습니다. –
수정 됨. 감사. – nekonaute