- 论坛徽章:
- 0
|
- #include <stdio.h>
- class A
- {
- public:
- virtual void test(){printf("A test\n");}
- };
- class B : public A
- {
- public:
- void func(){test();}
- virtual void test(){ printf("B test\n");}
- };
- class C : public B
- {
- public:
- virtual void test(){printf("C test\n");}
- };
- int main()
- {
- C c;
- //((B *)(&c))->func();
- C * pC = &c;
- B * pTmp = (B *)pC;
- printf( "&c: %p, (B *)&c: %p\n", pC, pTmp );
- (&c)->func();
- c.func();
- pTmp->func();
- (* pTmp).func();
- //((B)c).func();
- B insB = ((B)c);
- B * pB = &insB;
- printf( "&c: %p, &((B)c): %p\n", pC, pB );
- insB.func();
- (&insB)->func();
- (* pB).func();
- pB->func();
-
- return( 0 );
- }
复制代码
输出:
- &c: 0xbfe24a80, (B *)&c: 0xbfe24a80
- C test
- C test
- C test
- C test
- &c: 0xbfe24a80, &((B)c): 0xbfe24a60
- B test
- B test
- B test
- B test
复制代码 |
|