虚函数总结

 

#include <iostream>

#include <string>

using namespace std;

//normal class

class CBase1

{

public:

void Func1();

};

//class with virtual function

class CBase2

{

public:

virtual void Func1(); //virtual function

};

//class with pure virtual function

class CBase3

{

public:

virtual void Func1() = 0; //pure virtual function

};

//class derived from CBase2

class CDerived : public CBase2

{

public:

void Func1(); //this function is derived from CBase3

};

//implementations

void CBase1::Func1()

{

cout << "CBase1::Func1()" << endl;

}

void CBase2::Func1()

{

cout << "CBase2::Func1()" << endl;

}

void CBase3::Func1()

{

cout << "CBase3::Func1()" << endl;

}

void CDerived::Func1()

{

cout << "CDerived::Func1()" << endl;

cout << " call base::Func1" << endl;

cout << " ";

CBase2::Func1();

}

//class with pure virtual functions (even derived from parent calss, but not implement by himself) cannot be initialized

void TestInitialization()

{

cout << "TestInitialization() >>> " << endl;

//this is ok

CBase1 base1;

//this is also ok.

CBase2 *pBase2 = new CBase2;

delete pBase2;

//this is an error for compiling, so comment

//CBase3 base3;

//CBase3 *pBase3 = new CBase3;

}

//virtual functions can only be availabe via pointer or refrence

void CallingFunction( CBase2* pBase, CBase2& rBase, CBase2 oBase)

{

cout << "CallingFunction (CBase2*, CBase2&, CBase2 >>>" << endl;

cout << " by ptr : " ; pBase->Func1();

cout << " by ref : " ; rBase. Func1();

cout << " by obj : " ; oBase. Func1(); //子类对象作为参数传给父类,实际调用的是父类对象。}

void TestCallingFunction()

{

cout << "TestCallingFunction() >>>" << endl;

CDerived obj;

CallingFunction (&obj, obj, obj);

}

//test the difference of the size

void TestSizeof()

{

cout << "TestSizeof() >>> " << endl;

cout << " sizeof(CBase1) = " << sizeof(CBase1) << endl; //答案是1,因为该类虽没有任何成员,但是还是编译器还是会为其分配一个字节的内存。

cout << " sizeof(CBase2) = " << sizeof(CBase2) << endl; //答案是4,因为该类定义了一个虚函数,一个类中只要有了一个虚函数,编译器就会为其定义一个虚函数表VTABLE。虚函数表实际上是一个函数指针(指向虚函数)的数组。在创建类实例的时候,编译器会在类实例的内存布局中创建一个vptr字段,该字段指向本类的虚函数表。有了Vptr这个指针,所以类的字节数为4。

cout << " sizeof(CBase3) = " << sizeof(CBase3) << endl;

cout << " sizeof(CDerived) = " << sizeof(CDerived) << endl;

}

//the added pointer is _vptr which pointer to vtable and be initialized by compiler

//and normally, _vptr is added into the first place automatically by compiler

//call funtions via _vptr, this is abnormal, but showing the mechanism of vtable and vptr

void TestCallFunctionViaVptr()

{

cout << "TestCallFunctionViaVptr() >>>" << endl;

typedef void (*FUNC_PTR)(); //定义一个指向函数的指针,该函数没有参数,且返回类型为空。

 

CDerived obj;

//get the member _vptr

int ** vptr = (int**)(&obj); //obj内存布局中第一个值是一个指针,指向虚函数表。虚函数表存放的是虚函数地址。所以此处用指向指针的指针。

//function ptr to CDerive::Func1

FUNC_PTR pfunc1 = (FUNC_PTR)((*vptr)[0]); //this is not alwasy ZERO, change with compiler, for example, GCC and MS VC  //vptr 存放的是虚函数表的地址。(*vptr)[0]是虚函数。

//call functions

(*pfunc1)(); //this is same as obj.Func1(); //调用虚函数表中存放的虚函数。

}

//show the difference of virtual destructor, first class is with virual destrucotr, the other one is not.

//find the difference when their objects are deleted

class C1

{

public:

virtual ~C1 () { cout << "C1::~C1()" << endl; } //virtual

};

class C2 : public C1

{

public:

~C2 () { cout << "C2::~C2()" << endl; }

};

class D1

{

public:

~D1 () { cout << "D1::~D1()" << endl; } // not virtual

};

class D2 : public D1

{

public:

~D2 () { cout << "D2::~D2()" << endl; }

};

void TestVirtualDestructor()

{

cout << "TestVirtualDestructor() >>>" << endl;

//create the derived classes object

C1 *pC = new C2;

D1 *pD = new D2;

//delete object with virtual destructor via base pointer,

//then corrected destructor will be called, means child destructor fistly, then parent destructor

cout << "delte obj with virtual destructor " << endl; delete pC; pC = 0; //如果是虚析构,先析构子类,再析构父类。

//delete object without virtual destrucotr via base pointer,

//only the destructor of the type of the pointer will be called, this is not corrected

//memory leak happening here

cout << "delte obj without virtual destructor " << endl; delete pD; pD = 0;//如果不是虚析构,就只析构本类。

}

//program entry point, call the test functions above

int main(int argc, char** argv)

{

TestInitialization (); cout << endl;

TestCallingFunction (); cout << endl;

TestSizeof (); cout << endl;

TestCallFunctionViaVptr (); cout << endl;

TestVirtualDestructor(); cout << endl;

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值