c++ (virtual)虚函数

什么是虚函数

1.      当我们提到虚函数或虚方法时,我们通常指会指的是在继承或多态的背影下,基类虚函数的行为可以背子类定制。

2.      虚函数与非函数的最大区别是,虚函数是在运行时根据调用对象的不同动态邦定的;而非虚数是在编译时就已经决定定由哪个对象调用了,这种行为叫静态绑定。

虚函数使用说明

1.      非虚函数

#include<iostream>
 
using namespacestd;
 
class Base
{     
public:
       virtual void MethodA() {
              cout << "Base::voidMethodA()" << endl;
       }
       virtual void MethodA(int a) {
              cout << "Base::voidMethodA(int a)" << endl;
       }
};
 
class Derived :public Base
{
public:
       virtual void MethodA() {
              cout << "Derived::voidMethodA()" << endl;
       }
};
 
int main()
{
       Derived d;
       d.MethodA();
       d.MethodA(4);
       return 0;
}

输出结果:

因为f()是非函数,调用f()的对象是在编译就静态绑定了,所以前面三个输出的全是各类自身的f()函数。

((B *)c)->f() 输出为B::f()是因为c 是B的一个子类,在编译时由编译器静态绑定B::f()操作。

2、动态绑定代码

#include <iostream>

class A {
public:
	virtual void f() { 
		std::cout << "A::f()" << std::endl;
	}
};

class B: public A {
public:
	void f() { 
		std::cout << "B::f()" << std::endl;
	}
};

class C: public B {
public:
	void f() { 
		std::cout << "C::f()" << std::endl;
	}
};

int main()
{
	A *a = new A(); 
	B *b = new B(); 
	C *c = new C();

	a->f();			// A::f()
	b->f();			// B::f()
	c->f();			// C::f()

	((B *)c)->f();		// C::f()
	((A *)c)->f();		// C::f()
	((A *)b)->f();		// B::f()

	return 0;
}

 输出结果:


此时f()是虚函数,虚函数的调用是在运行时自动绑定的。当我们再次执行: ((B *)c)->f() 时因为c是类C的对象,所以运行时自动绑定C::f()操作。


3、动态绑定与静态绑定的区别

#include <iostream>
using namespace std;

class Base {
public:
	void f();
	virtual void vf();
};

class Derived : public Base {
public:
	void f();
	void vf();
};


void Base::f() {
	cout << "Base f()" << endl;
}

void Base::vf() {
	cout << "Base vf()" << endl;
}

void Derived::f() {
	cout << "Derived f()" << endl;
}

void Derived::vf() {
	cout << "Derived vf()" << endl;
}

int main()
{
	Base b1;
	Derived d1;
	b1.f();
	b1.vf();
	d1.f();
	d1.vf();

	Derived d2;  	// Derived object
	Base* bp = &d2; // Base pointer to Derived object
	bp->f();	// Base f()
	bp->vf();	// which vf()?

	return 0;
}

输出结果:


通过以上代码可以得出以下结论:

1.      指针或引用的类型是在编译时就知道了,但是具体由哪个对象调用需要在运行时根据类型动态决定。

2.      如果一个函数在基类中为虚函数,在所有继承此基类的子类中全部为虚函数。

3.      如果一个虚函数被一个对象的引用或指针调用,调用此虚函数的对象应该是定义此函数或指针的而不是定声明此函数或指针的。

4.      如果一个基类中有需要被子类重新定义的方法,我们需要将此方法声明为虚函数。

5.      运行时动态绑定是虚函数的主要优势,但任何事情都有另一面调用函数需要比调用非虚函数需要更多的时间及内存。因为虚数在运行时调用,调用前必需查询一个虚函数表动态的调用适当的对象,而百虚函数在编译时已经确定。有虚函数的对象比没有需函数的对象需要多一个指象需函数表的指针,如果要创建非常多爱而且非常小的对象就会多上用很多内存。但在一般工程环境中以上两个缺点可以忽略。

6.      在一个类中如果添加、修改或改类虚函数的声明顺序都需要重新进行编译,因为虚函数是通过基类在虚函数表中的偏移量位置来查询的。

虚析构函数

如果一个用来做基类,这个基类的析构函数应该被定义为虚函数。如果一个类没有包括任何虚函数,那为就意味着这个类不会用来做基类。

为什么如果我们的类中有虚函数,我们就需要一个虚的析构函数呢?以下通过代码说明:

1.      基类的析构函数不是虚函数时的情况:

#include <iostream>

using namespace std;

class Base
{
public:
	Base() {
		cout << "Base Constructor \n" ;
	}
	~Base() {
		cout << "Base Destructor \n" ;
	}
};

class Derived : public Base
{
public:
	Derived(string s):str(s) {
		cout << "Derived Constructor \n" ;
	}
	~Derived() {
		cout << "Derived Destructor \n" ;
	}
private:
	string str;
};

int main()
{
	Base *pB = new Derived("derived");
	delete pB;
	return 0;
}

输出结果:


基类的析构函数没有被调用,也就是说当基类析构有需要释放的资源时就发生资源泄露。

2.      当基类的析构函数是虚函数时:


#include <iostream>

using namespace std;

class Base{
protected:
	int myInt;

public:
	Base(int n):myInt(n){
		cout << "Base Ctor\n";
	}
	virtual void print() const = 0;
	virtual ~Base(){
		cout << "Base Dtor" << endl;
	}
};

class Derived: public Base {
public:	
	Derived(int n = 0):Base(n)  {
		str = new char[100];
		myInt = n;				
		cout << "Derived Ctor myInt" << endl;
	}
	void print()const{
		cout << "Derived print(): myInt = "<< myInt << endl;
	}

	~Derived(){
		cout << "Derived Dtor" << endl;
		delete [] str;
	}
private:
	char *str;
};


int main()
{
	Base *pB = new Derived(2010);
	pB->print();
	delete pB;
	return 0;
}

输出结果:


根据输出结果可以得知基类的析构函数被自动调用。

虚构造函数

构造函数不能为虚函数,因为创建一个子类的对象,执行子类的构造函数,然后子类的构造函数调用基础的构适函数,但如果构造函数是虚函数子类就可能不知道具体的基类类型。因此不能构造成功。

纯虚函数

纯虚函数有两种定义方式:

virtual void vf() = 0 或 virtual ~Interface() = 0; 

纯虚函数目的:

1.      纯虚函数只能做为子类的接口,基类不能定义任何对象。

2.      子类必需实现基类的每一个纯虚函数。

基类虚重载函数隐藏

如果基类定义了虚重载函数,如果子类重新定义了一个。就要重新定义此函数的所有重载版本。

代码如下:

#include <iostream>

using namespace std;

class Base 
{	
public:
	virtual void MethodA() {
		cout << "Base::void MethodA()" << endl;
	}
	virtual void MethodA(int a) {
		cout << "Base::void MethodA(int a)" << endl;
	}
};

class Derived : public Base
{
public:
	virtual void MethodA() {
		cout << "Derived::void MethodA()" << endl;
	}
};

int main() 
{
	Derived d;
	d.MethodA();
	d.MethodA(4);
	return 0;
}

编译错误如下:

error C2660: 'Derived::MethodA' :function does not take 1 arguments

进行修正,子类实现所有版本的重载

#include <iostream>

using namespace std;

class Base 
{	
public:
	virtual void MethodA() {
		cout << "Base::void MethodA()" << endl;
	}
	virtual void MethodA(int a) {
		cout << "Base::void MethodA(int a)" << endl;
	}
};

class Derived : public Base
{
public:
	virtual void MethodA() {
		cout << "Derived::void MethodA()" << endl;
	}
	virtual void MethodA(int a) {
		cout << "Derived::void MethodA(int a)" << endl;
	}
};

int main() 
{
	Derived d;
	d.MethodA();
	d.MethodA(4);
	return 0;
}

输出结果:


动态转换

运行时类型实别(RTTI)

RTTI 是 Run-time Type Identification 的缩写。RTTI为程序提供了一个标准方式用来在运行时决定对象的类型。也就是说RTTI允许指向基类的对象或引用在运行时恢复到其实际指的子类类弄。

通过以下两种方式提供RTTI操作:

l  通过typeid操作符,来返回通过指针或引用实际指的对象类型。

l  通过dynamic_cast操作符,安全的将基类指针或引用转为子类的指针或引用类型。

dynamic_cast 操作符

dynamic_cast 操作符在RTTI操作中经常用到,它是用来告诉我们是否能够全安的将一个对象的地址指针转换为一个具体的类型。

以下通过代码进行说明:

class Base { };

class Derived : public Base { };

int main() 
{
	Base b;
	Derived d;

	Base *pb = dynamic_cast<Base*>(&d);      	// #1
	Derived *pd = dynamic_cast<Derived*>(&b); 	// #2

	return 0;
}

#1 可能通过编译,因为将子类转换成基类dynamic_cast永远成功

#2 编译失败因为如果不是多态方式下,dynamic_cast不允许将基类转换成子类。

向上转型或向转型

将子类的指针或引用转换成基类的指针或引用叫到向上转型,在公有继承中不需要特别处理类型转换。实个规则可以认为是is-a 的对象关系。一个子类对象继承了基类的所有数据及函数。基类能做的事情,子类也可以做。

向下转型与向上转型相反,是用来将一个基类对象的指针或引用转为子类对象的指针或引用。子类可能增加新的类成员,但这些新增加的类成员不能在基类中使用。

代码说明:

#include <iostream>
using namespace std;

class Employee { 
private:
	int id;
public:
	void show_id(){}
};

class Programmer : public Employee {
public:
	void coding(){}
};

int main() 
{
	Employee employee;
	Programmer programmer;

	// upcast - implicit upcast allowed
	Employee *pEmp = &programmer;

	// downcast - explicit type cast required
	Programmer *pProg = (Programmer *)&employee;


	// Upcasting: safe - progrommer is an Employee 
	// and has his id to do show_id().
	pEmp->show_id();
	pProg->show_id();

	// Downcasting: unsafe - Employee does not have
	// the method, coding().
       // compile error: 'coding' : is not a member of 'Employee'
	// pEmp->coding(); 
	pProg->coding();

	return 0;
}

typeid操作符

我们可以通过typeid操作符来检测两个对象类型是否相同。

代码如下:

#include <iostream>
#include <typeinfo>
using namespace std;

class Employee { 
private:
	int id;
public:
	void show_id(){}
};

class Programmer : public Employee {
public:
	void coding(){}
};

int main() 
{
	Employee lee;
	Programmer park;

	Employee *pEmpA = &lee;
	Employee *pEmpB = &park;

	// check if two object is the same
	if(typeid(Programmer) == typeid(lee)) {
		Programmer *pProg = (Programmer *)&lee; 
		pProg->coding();
	}
	if(typeid(Programmer) == typeid(park)) {
		Programmer *pProg = (Programmer *)&park; 
		pProg->coding();
	}

	pEmpA->show_id();
	pEmpB->show_id();

	return 0;
}

类型转换

类型转换操作

a = dynamic_cast<T*>(p) 
Try to convert p into a T*. It may return 0
a = dynamic_cast<T&>(*p) 
Try to convert *p into a T&. It may throw bad_cast

a = static_cast<T>(p) 
Convert p into a T if a T can be conterted into p's type.

a = reinterpret_cast<T>(p) 
Convert p into a T represented by the same bit pattern.

a =const_cast<T>(p) 
Convert p into a T by adding or subtracting const.

a = (T)v 
C-style cast.

a = T(v) 
Functional cast



 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值