标签:class com code img size http div string tar style width
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 |
classA { public : voidprint(){cout<< "ThisisA" <<endl;} }; classB:publicA { public : voidprint(){cout<< "ThisisB" <<endl;} }; intmain() { //为了在以后便于区分,我这段main()代码叫做main1 Aa; Bb; a.print(); b.print(); } |
1
2
3
4
5
6
7
8 |
intmain(){ //main2 Aa; Bb; A*p1=&a; A*p2=&b; p1->print(); p2->print(); } |
1
2
3
4
5
6
7
8 |
classA{ public : virtualvoidprint(){cout<< "ThisisA" <<endl;} //现在成了虚函数了 }; classB:publicA{ public : voidprint(){cout<< "ThisisB" <<endl;} //这里需要在前面加上关键字virtual吗? }; |
1
2
3
4
5
6
7
8
9
10 |
classA{ //虚函数示例代码 public : virtualvoidfun(){cout<<1<<endl;} virtualvoidfun2(){cout<<2<<endl;} }; classB:publicA{ public : voidfun(){cout<<3<<endl;} voidfun2(){cout<<4<<endl;} }; |
1
2
3
4
5
6
7
8
9
10
11
12
13 |
#include<iostream> usingnamespacestd; //将上面“虚函数示例代码”添加在这里 intmain(){ void (*fun)(A*); A*p=newB; longlVptrAddr; memcpy (&lVptrAddr,p,4); memcpy (&fun, reinterpret_cast < long *>(lVptrAddr),4); fun(p); deletep; system ( "pause" ); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 |
#include<iostream> usingnamespacestd; classA{ //虚函数示例代码2 public : virtualvoidfun(){cout<< "A::fun" <<endl;} virtualvoidfun2(){cout<< "A::fun2" <<endl;} }; classB:publicA{ public : voidfun(){cout<< "B::fun" <<endl;} voidfun2(){cout<< "B::fun2" <<endl;} }; //end//虚函数示例代码2 intmain(){ void (A::*fun)(); //定义一个函数指针 A*p=newB; fun=&A::fun; (p->*fun)(); fun=&A::fun2; (p->*fun)(); deletep; system ( "pause" ); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 |
#include<iostream> usingnamespacestd; //将上面“虚函数示例代码2”添加在这里 voidCallVirtualFun( void *pThis,intindex=0){ void (*funptr)( void *); longlVptrAddr; memcpy (&lVptrAddr,pThis,4); memcpy (&funptr, reinterpret_cast < long *>(lVptrAddr)+index,4); funptr(pThis); //调用 } intmain(){ A*p=newB; CallVirtualFun(p); //调用虚函数p->fun() CallVirtualFun(p,1); //调用虚函数p->fun2() system ( "pause" ); } |
标签:class com code img size http div string tar style width
原文地址:http://www.cnblogs.com/duyy/p/3694685.html