#include <iostream>
using namespace std;
class Person
{
public:
void P(){ cout << "Person P" << endl; }
virtual void Print(){ cout << "Person Print" << endl; }
Person(){ cout << "Constructor Person" << endl; }
virtual ~Person(){ cout << "Dectructor Person" << endl; }
};
class Developer : public Person
{
public:
void Pi() { cout << "Developer Pi" << endl; }
void Print() override
{
cout << "Developer Print" << endl;
}
Developer(){ cout << "Constructor Develoeper" << endl; }
~Developer(){ cout << "Dectructor Develoer" << endl; }
};
int main()
{
Person *p = new Person();
Developer* d = dynamic_cast<Developer*>(p);
d->Pi();
delete p;
delete d;
return 0;
}
출력 :파생 클래스의 방법
Constructor Person
Developer Pi
Dectructor Person
왜 내가 Developer
의 기능 Pi
호출 할 수 있습니다?
Developer
님의 생성자 없이는 Pi
을 어떻게 호출 할 수 있습니까?
Pi
은 Developer
클래스에서만 선언됩니다.
원하는대로 할 수 있지만 ** 정의되지 않은 동작이 발생합니다 ** – Rakib
이것이 UB 인 이유에 대한 매우 철저한 답을보십시오. http://stackoverflow.com/a/2474021/3549027 – dlf