Oli Charlesworth가 지적했듯이 가상 포인터는 구현 세부 사항이므로이 질문은 C++ 측면에서 실제로 이해가되지 않습니다. 즉 가상 함수의 다음과 같은 수동 구현 (일부 기능이) 이해에 도움이 될 수 말했다
물론
struct vtable {
void (*display)(void*);
void (*setValue)(void*, int);
};
void A_display(void *this_) { /*Cast this_ to A* and do A stuff*/ }
void A_setValue(void *this_, int x) { /*Cast this_ to A* and do A stuff*/ }
vtable A_vtable = {A_display, A_setValue};
struct A {
vtable *vptr = &A_vtable;
int a;
public: A(){}
};
void B_display(void *this_) { /*Cast this_ to B* and do B stuff*/ }
void B_setValue(void *this_, int x) { /*Cast this_ to B* and do B stuff*/ }
vtable B_vtable = {B_display, B_setValue};
struct B {
vtable *vptr = &B_vtable;
int a;
public: B(){}
};
void display(void *obj) {
((*static_cast<vtable**>(obj))->display)(obj);
}
void setValue(void *obj, int) {
((*static_cast<vtable**>(obj))->setValue)(obj, int);
}
, 이것은 단지 가상 함수의 기능의 작은 부분 집합을 제공하지만, 그것은해야 vptrs
은 고정 된 유형의 함수에 대한 포인터 모음을 가리 킵니다.
가능한 복제본 [C++의 가상 테이블 구조 란 무엇입니까?] (http://stackoverflow.com/questions/5868431/what-is-the-structure-of-virtual-tables-in-c) –