class TestParent {
public:
virtual void foo(void) {
printf("call TestParent::foo()\n");
}
virtual void bar(void) {
printf("call TestParent::bar()\n");
}
public:
int m_nMember1;
};
class TestChild1
: public TestParent {
public:
void foo(void) {
printf("call TestChild1::foo()\n");
}
int m_nMember2;
};
class TestChild2
: public TestParent {
public:
void foo(void) {
printf("call TestChild2::foo()\n");
}
int m_nMember2;
};
class TestChild3
: public TestParent {
public:
void foo(void) {
printf("call TestChild3::foo()\n");
}
int m_nMember2;
};
int main() {
std::vector vTestCls;
vTestCls.push_back(new TestParent);
vTestCls.push_back(new TestChild1);
vTestCls.push_back(new TestChild2);
vTestCls.push_back(new TestChild3);
for(auto p = begin(vTestCls); p != end(vTestCls); p++) {
TestParent* pTestParent = *p;
pTestParent->foo();
pTestParent->bar();
}
void (TestParent::*pfn_parent_foo)(void);
void (TestChild1::*pfn_child1_foo)(void);
void (TestChild2::*pfn_child2_foo)(void);
void (TestChild2::*pfn_child3_foo)(void);
TestChild1* pTestChild1 = new TestChild1;
pfn_child1_foo = &TestChild1::foo;
for(auto p = begin(vTestCls); p != end(vTestCls); p++) {
pfn_parent_foo = &TestParent::foo;
// 함수의 호출 주소만 가짐(인스턴스 X)
TestParent* pTestParent = *p;
pTestParent->m_nMember1 = 0x41414141;
(pTestParent->*pfn_parent_foo)();
// 인스턴스에서 함수의 주소를 이용하여 해당 함수를 호출
// 결국 vtable을 또 거치네
pTestParent->bar();
}
return 0;
}