당신은이에 대한 std::dynamic_cast
를 사용할 수 있습니다. 당신이 std::shared_ptr
처럼, 스마트 포인터를 사용하는 경우 또한
Parent* ptr = new Child1();
if(dynamic_cast<Child1*>(ptr) != nullptr) {
// ptr is object of Child1 class
} else if(dynamic_cast<Child2*>(ptr) != nullptr) {
// ptr is object of Child2 class
}
,이처럼 확인할 수 있습니다
std::shared_ptr<Parent> ptr(new Child1());
if(std::dynamic_pointer_cast<Child1>(ptr) != nullptr) {
// ptr is object of Child1 class
} else if(std::dynamic_pointer_cast<Child2>(ptr) != nullptr) {
// ptr is object of Child2 class
}
'부모 *'는 항상 '부모 *'입니다. 결코 * Child *가 아닙니다. 당신이 묻는 것은 "그것이 가리키는 것의 유형은 무엇입니까"입니다. –
Kerrek SB : 어제 차가운 어깨를 줬어.하지만 실제 프로그래밍을하다가 너의 의견이 페이지의 다른 어떤 것보다 더 중요했을거야. 죄송합니다 ... – ExOttoyuhr