#include <stdio.h>typedefvoid(*Fun)(void);
class Child{
public:
int age;
Child():age(8){}
virtualvoid who(){ printf("I am child\n");}
virtualvoid study(){ printf("study...\n");}
};
int main(){
Child child;
Fun fun = NULL;
int * vtbl = (int *)(*((int*)&child));
printf("child(0x%p) : size(%d)\n",&child,sizeof(child));
printf("child[0] is vptr, points to vtbl 0x%p\n",vtbl);
printf("child[1] is Age : %d\n", ((int *)(&child))[1]);
for (int i=0; (Fun)vtbl[i]!=NULL;i++){
fun = (Fun)vtbl[i];
printf("vtbl[%d] : ",i);
fun();
}
}
打印结果
child(0x0012FF5C) : size(8)
child[0] is vptr, points to vtbl 0x0041587C
child[1] is Age : 8
vtbl[0] : I am child
vtbl[1] : study...
内存布局
ChildSize:8B
|vptr|age|
内存布局
ChildSize:8B
|vptr|age|
其他
Vtbl存在堆上
单一继承
#include <stdio.h>typedefvoid(*Fun)(void);
class GrandFather{
public:
GrandFather():age(60){}
void fishing(){ printf("GrandFather go to fishing...\n");}
int age;
};
class Father:public GrandFather{
public:
Father():age(36){}
void cutting(){ printf("Father go to cutting...\n");}
int age;
};
class Child:public Father{
public:
Child():age(8){}
void studying(){ printf("Child go to studying...\n");}
int age;
};
int main(){
Child child;
printf("child(0x%p) : size(%d)\n",&child,sizeof(child));
printf("child[0] is GrandFather : %d\n", ((int *)(&child))[0]);
printf("child[1] is Father : %d\n", ((int *)(&child))[1]);
printf("child[2] is child : %d\n", ((int *)(&child))[2]);
}
打印结果
child(0x0012FF58) : size(12)
child[0] is GrandFather : 60
child[1] is Father : 36
child[2] is child : 8
内存布局
Child Size:12B
|GrandFather::age|
|Father::age|
|Child::age|
单一继承&多态
#include <stdio.h>typedefvoid(*Fun)(void);
class GrandFather{
public:
GrandFather():age(60){}
virtualvoid who(){ printf("I am GrandFather\n");}
virtualvoid fishing(){ printf("GrandFather go to fishing...\n");}
virtualvoid hungry(){ printf("GrandFather is hungry\n");}
int age;
};
class Father:public GrandFather{
public:
Father():age(36){}
virtualvoid who(){ printf("I am Father\n");}
virtualvoid cutting(){ printf("Father go to cutting...\n");}
virtualvoid hungry(){ printf("Father is hungry\n");}
int age;
};
class Child : public Father{
public:
Child():age(8){}
virtualvoid who(){ printf("I am Child\n");}
virtualvoid studying(){ printf("Child go to studying...\n");}
virtualvoid hungry(){ printf("Child is hungry\n");}
int age;
};
int main(){
Child child;
Fun fun = NULL;
int * vtbl = (int *)(*((int*)&child));
printf("child(0x%p) : size(%d)\n",&child,sizeof(child));
printf("child[0] is vptr, points to vtbl 0x%p\n",vtbl);
printf("child[1] is GrandFather : %d\n", ((int *) (&child[1]));
printf("child[2] is Father : %d\n", ((int *)(&child))[2]);
printf("child[3] is child : %d\n", ((int *)(&child))[3]);
for (int i=0; (Fun)vtbl[i]!=NULL;i++){
fun = (Fun)vtbl[i];
printf("vtbl[%d] : ",i);
fun();
}
}
打印结果
child(0x0012FF54) : size(16)
child[0] is vptr, points to vtbl 0x004157FC
child[1] is GrandFather : 60
child[2] is Father : 36
child[3] is child : 8
vtbl[0] : I am Child
vtbl[1] : GrandFather go to fishing...
vtbl[2] : Child is hungry
vtbl[3] : Father go to cutting...
vtbl[4] : Child go to studying...
内存布局
Child Size:16B
|vptr|
|GrandFather::age|
|Father::age|
|Child::age|
Vtbl
|Child::who()|
|GrandFather::fishing()|
| Child::hungry()|
|Father::cutting()|
|Child::studying()|
多重继承
#include <stdio.h>
class GrandFather{
public:
GrandFather():age(60){}
int age;
};
class Grandad{
public:
Grandad():age(57){}
int age;
};
class Father:public GrandFather{
public:
Father():age(36){}
int age;
};
class Mother:public Grandad{
public:
Mother():age(34){}
int age;
};
class Child : public Father, public Mother{
public:
Child():age(8){}
int age;
};
int main(){
Child child;
int* pchild = (int *)&child;
printf("child(0x%p) : size(%d)\n",&child,sizeof(child));
printf("child[0] GrandFather : %d\n",pchild[0]);
printf("child[1] Father : %d\n",pchild[1]);
printf("child[2] Grandad : %d\n",pchild[2]);
printf("child[3] Mother : %d\n",pchild[3]);
printf("child[4] Child : %d\n",pchild[4]);
}
#include <stdio.h>typedefvoid(*Fun)(void);
class GrandFather{
public:
GrandFather():age(60){}
virtualvoid who(){
printf("I am GrandFather\n");
}
virtualvoid fishing(){
printf("GrandFather go to fishing...\n");
}
virtualvoid hungry(){
printf("GrandFather is hungry\n");
}
int age;
};
class Grandad{
public:
Grandad():age(57){}
virtualvoid who(){
printf("I am Grandad\n");
}
virtualvoid chessing(){
printf("Grandad go to chessing...\n");
}
virtualvoid hungry(){
printf("Grandad is hungry\n");
}
int age;
};
class Father:public GrandFather{
public:
Father():age(36){}
virtualvoid who(){
printf("I am Father\n");
}
virtualvoid cutting(){
printf("Father go to cutting...\n");
}
virtualvoid hungry(){ printf("Father is hungry\n");
}
int age;
};
class Mother:public Grandad{
public:
Mother():age(34){}
virtualvoid who(){
printf("I am Mother\n");
}
virtualvoid sewing(){
printf("Mother go to sewing...\n");
}
virtualvoid hungry(){ printf("Mother is hungry\n");
}
int age;
};
class Child : public Father, public Mother{
public:
Child():age(8){}
virtualvoid who(){
printf("I am Child\n");
}
virtualvoid studying(){
printf("Child go to studying...\n");
}
virtualvoid hungry(){
printf("Child is hungry\n");
}
int age;
};
int main(){
Child child;
int* pchild = (int *)&child;
printf("child(0x%p) : size(%d)\n",&child,sizeof(child));
printf("child[0] vptr1 : 0x%p\n",pchild[0]);
printf("child[1] GrandFather : %d\n",pchild[1]);
printf("child[2] Father : %d\n",pchild[2]);
printf("child[3] vptr2 : 0x%p\n",pchild[3]);
printf("child[4] Grandad : %d\n",pchild[4]);
printf("child[5] Mother : %d\n",pchild[5]);
printf("child[6] Child : %d\n",pchild[6]);
Fun fun = NULL;
int * vtbl1 = (int *)pchild[0];
for (int i=0; (Fun)vtbl1[i]!=NULL; i++){
fun = (Fun)vtbl1[i];
printf("vtbl1[%d] : ", i);
fun();
}
int * vtbl2 = (int *)pchild[3];
for (int i=0; (Fun)vtbl2[i]!=NULL; i++){
fun = (Fun)vtbl2[i];
printf("vtbl2[%d] : ", i);
fun();
}
getchar();
}
打印结果
child(0x0012FF48) : size(28)
child[0] vptr1 : 0x00416864
child[1] GrandFather : 60
child[2] Father : 36
child[3] vptr2 : 0x0041684C
child[4] Grandad : 57
child[5] Mother : 34
child[6] Child : 8
vtbl1[0] : I am Child
vtbl1[1] : GrandFather go to fishing...
vtbl1[2] : Child is hungry
vtbl1[3] : Father go to cutting...
vtbl1[4] : Child go to studying...
vtbl2[0] : I am Child
vtbl2[1] : Grandad go to chessing...
vtbl2[2] : Child is hungry
vtbl2[3] : Mother go to sewing...
内存布局
Child Size:28B
|vptr1||GrandFather::age||Father::age||vptr2||Grandad::age||Mother::age||Child::age|