最近在做流媒体的应用,用的苹果的Darwin Streaming Server做二次开发,恶补一下C++。多态是C++的精髓。
C++中的虚函数是实现多态性的重要手段,它是通过虚函数表来实现的,虚函数表放在类的首地址位置,具体为:
*(int*)(&b) :其中b为一个类的实例(对象);
下面写的是对上面做出直观诠释的代码:
#include <iostream>
//单继承
class Base {
public:
int a,b,c;
virtual void f() { std::cout << "Base::f" << std::endl; }
virtual void g() { std::cout << "Base::g" <<std::endl; }
virtual void h() { std::cout << "Base::h" << std::endl; }
};
class Derive:public Base
{
public:
virtual void f0() { std::cout << "Base::f0" << std::endl; }
virtual void g0() { std::cout << "Base::g0" <<std::endl; }
virtual void h0() { std::cout << "Base::h0" << std::endl; }
};
class Derive1:public Base
{
public:
virtual void f() { std::cout << "Base::f1" << std::endl; }
virtual void g() { std::cout << "Base::g1" <<std::endl; }
virtual void h() { std::cout << "Base::h1" << std::endl; }
};
//多继承的例子:
class Base1{
public:
int a,b,c;
virtual void f() { std::cout << "Base1::f" << std::endl; }
virtual void g() { std::cout << "Base1::g" <<std::endl; }
virtual void h() { std::cout << "Base1::h" << std::endl; }
};
class Base2{
public:
int a,b,c;
virtual void f() { std::cout << "Base2::f" << std::endl; }
virtual void g() { std::cout << "Base2::g" <<std::endl; }
virtual void h() { std::cout << "Base2::h" << std::endl; }
};
class Base3{
public:
int a,b,c;
virtual void f() { std::cout << "Base3::f" << std::endl; }
virtual void g() { std::cout << "Base3::g" <<std::endl; }
virtual void h() { std::cout << "Base3::h" << std::endl; }
};
class DeriveMulti:public Base1,public Base2,public Base3
{
virtual void f() { std::cout << "DeriveMulti::f" << std::endl; }
// virtual void g() { std::cout << "DeriveMulti::g" <<std::endl; }
// virtual void h() { std::cout << "DeriveMulti::h" << std::endl; }
virtual void mm() { std::cout << "DeriveMulti::mm" << std::endl; }
};
typedef void(*Fun)(void);
int main(void)
{
Base b;
Derive d;
Derive1 d1;
Fun pFun = NULL;
Base *pb;
Base1 *pb1;
Base2 *pb2;
Base3 *pb3;
DeriveMulti *pdm;
std::cout <<"\n\n@@@@ Base TEST @@@@@" << std::endl;
std::cout << "虚函数表地址:" << *(int*)(&b) << std::endl;
std::cout << "虚函数表 — 第一个函数地址:" << *(int*)*(int*)(&b) << std::endl;
pFun = (Fun)*((int*)*(int*)(&b));
pFun();
pFun = (Fun)*((int*)*(int*)(&b)+2); // Base::g()
pFun();
pFun = (Fun)*((int*)*(int*)(&b)+4); // Base::h()
pFun();
b.f();
b.g();
b.h();
std::cout <<"\n\n@@@@ Dervie TEST @@@@@" << std::endl;
std::cout << "虚函数表地址:" << *(int*)(&d) << std::endl;
std::cout << "虚函数表 — 第一个函数地址:" << *(int*)*(int*)(&d) << std::endl;
pFun = (Fun)*((int*)*(int*)(&d));
pFun();
pFun = (Fun)*((int*)*(int*)(&d)+2); // Base::g()
pFun();
pFun = (Fun)*((int*)*(int*)(&d)+4); // Base::h()
pFun();
pFun = (Fun)*((int*)*(int*)(&d)+6); // Base::g()
pFun();
pFun = (Fun)*((int*)*(int*)(&d)+8); // Base::h()
pFun();
pFun = (Fun)*((int*)*(int*)(&d)+10); // Base::g()
pFun();
d.f();
d.g();
d.h();
d.f0();
d.g0();
d.h0();
std::cout <<"\n\n@@@@ Dervie 1 TEST @@@@@" << std::endl;
std::cout << "虚函数表地址:" << *(int*)(&d1) << std::endl;
std::cout << "虚函数表 — 第一个函数地址:" << *(int*)*(int*)(&d1) << std::endl;
pFun = (Fun)*((int*)*(int*)(&d1));
pFun();
pFun = (Fun)*((int*)*(int*)(&d1)+2); // Base::g()
pFun();
pFun = (Fun)*((int*)*(int*)(&d1)+4); // Base::h()
pFun();
d1.f();
d1.g();
d1.h();
std::cout <<"\n\n@@@@ Base pointer TEST @@@@@" << std::endl;
pb = &b;
pb->f();
pb->g();
pb->h();
pb = &d;
pb->f();
pb->g();
pb->h();
pb = &d1;
pb->f();
pb->g();
pb->h();
std::cout <<"\n\n@@@@ multiple inheritance TEST @@@@@" << std::endl;
pdm = new DeriveMulti();
pb1 = pdm;
pb1->f();
pb1->g();
pb1->h();
pb2=pdm;
pb2->f();
pb2->g();
pb2->h();
pb3=pdm;
pb3->f();
pb3->g();
pb3->h();
return 0;
} |