`

c++ - dynamic_cast revisit

    博客分类:
  • c++
c++ 
阅读更多

There are several built-in type of cast that is available, they are 

  1. reinterpret_cast
  2. static_cast
  3. dynamic_cast

in tihs topic let's take a close look at the dynamic_cast. 

 

why do we need the dynamic_cast. 

 

suppose that we have the following class hierarchy 

 

/** why do we need dynamic_cast
* consider the following case. 
*/

class employee
{
public: 
	virtual int salary(); 
};

class manager : public employee
{
public: 
	int salary();
};

class programmer : public employee
{
public: 
	int salary(); 
};

 What is the most common problem of programming, change, yes, it is.  suppose that we will have a bonus method to the employee class and only the programmer class has a meaningful implementation to the bonus method. now the class hierarchy has changed to this.

 

/* 
* now we will add more method to the class hierarchy above 
*/

class employee
{
public:
	virtual int salary(); 
	virtual int bonus();
};

class manager : public employee 
{
public:
	int salary();
};

class programmer : public employee
{
public:
	int salary(); 
	int bonus();
};

 now, to make the code work , we have to tell if employee pointer or referfence is a manager/programmer so that we can do things that is specific to the type of class themselves.

 

class company
{
public :
	void payroll(employee *pe);
	//void payroll(employee &re) ;
};

/**
* the 
*  dynamic_cast 
* operator therefore performans two operational at once, it verifies that the request cast is indeede valid, and then only if it
* is valid does it performs the cast. The verfication take place at 
*  compile time
* the dynamic_cast is safer than the other c++ cast operations because the other casts do no verify whether the cast can actually be performed. 
* 
* the dynamic_cast is used for safe casting from a pointer to base class to a pointer to a derived class, often referred to as safe downcasting. 
*/
void company::payroll(employee *pe) 
{
	// 
	programmer *pm = dynamic_cast<programmer *> (pe); 

	// if pe refers to an object of type programmer, 
	// the dynamic_cast is successful,
	// and pm refers to the start of the programmer object
	if (pm) { 
		// use pm to call programmer::bonus();
		// if pe not refer to an object of type programmer, 
		// the dynamic_cast fails, 
		// an pm has the vlaue 0 
	} else {
		// use of employee's member function 
	}
}

 

What is special about dynamic_cast 

so what is special about the dynamic_cast itself. 

 

The  dynamic_cast operator therefore performans two operational at once, it verifies that the request cast is indeede valid, and then only if it is valid does it performs the cast. The verfication take place at  runtime.

 

the dynamic_cast is safer than the other c++ cast operations because the other casts do no verify whether the cast can actually be performed. 

 

The dynamic_cast is used for safe casting from a pointer to base class to a pointer to a derived class, often referred to as safe downcasting.

 

dynamic_cast can be lvalue reference

what we have seen in the example are cases with pointer, it is not just the pointer that can be used in the dynamic_cast, the lvalue reference can be supported as well.

 

see the example below. 

 

 

/*
* dynamic_cast
* 
* can converts a pointer to a base class to a pointer to a derived class. A dynamic cast can also be used to convert an lvalule 
* of base class to a reference to a derived class the syntax is as follow 
*/
void dynamic_cast_references()
{
	//employee * pe = new employee();
	programmer * pp= new programmer();
	employee & emp = dynamic_cast<programmer&>(*pp);
}
 

error condition 

As we said before that dynamic_cast performs at the runtime, what if the cast fails, what will happens?  and how to handle and possibly recover from the error cast condition.

 

/** 
* error condition
* 
*/
#include <typeinfo>
void company::payroll(employee & re) 
{
	try { 
		programmer &rm = dynamic_cast<programmer &>( re); 
	} catch (std::bad_cast) { 
		// use of employee member functions 
	}
}
 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics