The dynamic_cast ambiguous C++ class object experiment code example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info:

To do: Demonstrating the ambiguous C++ class object casting using the dynamic_cast

To show: The dynamic_cast ambiguous class object casting experiment

 

// dynamic_cast ambiguous conversion experiment

#include <iostream>

using namespace std;

 

// a class with virtual function, polymorphic

class Base1

{

public:

virtual void FuncBase1()

{};

};

 

// another class with virtual function...

class Base2

{

public:

virtual void FuncBase2()

{};

};

 

// derived class from Base1 and Base2 classes public virtual and private...

class Derived1:public virtual Base1, private Base2

{};

 

// dynamic_cast test function...

void DynamicCastSample()

{

// instantiate an object of type Derived1 class...

Derived1 DerivedObj;

 

// simple assignment, derived to base class, upcasting...

// cast needed to break private protection...

Base2* Base2Obj = (Base2*) &DerivedObj;

// another assignment, derived to base class, upcasting...

// public inheritance, no need casting..

Base1* Base1Obj = &DerivedObj;

// base class to derived class, downcast

Derived1& Derived1Obj = dynamic_cast<Derived1>(*Base2Obj);

if(!&Derived1Obj)

cout<<"Conversion is failed!...."<<endl;

else

cout<<"Conversion is OK...."<<endl;

 

cout<<"The address.."<<&Derived1Obj<<endl;

 

// base class to derived class, downcast

Base1Obj = dynamic_cast<Base1>(Base2Obj);

if(!Base1Obj)

cout<<"Conversion is failed!...."<<endl;

else

cout<<"Conversion is OK...."<<endl;

cout<<"The address.."<<Base1Obj<<endl;

 

// base class to base class, ????

// no inheritance

Base2Obj = dynamic_cast<Base2>(Base1Obj);

if(!Base2Obj)

cout<<"Conversion is failed!...."<<endl;

else

cout<<"Conversion is OK...."<<endl;

 

cout<<"The address.."<<Base2Obj<<endl;

 

// derived class to base class, upcast

Base1Obj = dynamic_cast<Base1>(&Derived1Obj);

if(!Base1Obj)

cout<<"Conversion is failed!...."<<endl;

else

cout<<"Conversion is OK...."<<endl;

 

cout<<"The address.."<<Base1Obj<<endl;

// derived class to base class...

// Derived1Obj is derived from non-virtual, private Base2

Base2Obj = dynamic_cast<Base2>(&Derived1Obj);

if(!Base2Obj)

cout<<"Conversion is failed!...."<<endl;

else

cout<<"Conversion is OK...."<<endl;

cout<<"The address.."<<Base2Obj<<endl;

}

 

int main(void)

{

int *ptr = NULL;

int var;

 

cout<<"Benchmarking..."<<endl;

cout<<"Address of var = "<<&var<<endl;

 

// NULL pointer

cout<<"NULL *ptr = "<<ptr<<endl;

cout<<endl;

 

// call the function for dynamic_cast testing...

DynamicCastSample();

 

return 0;

}

 

Output example:

 

Benchmarking...

Address of var = 0012FF54

NULL *ptr = 00000000

Press any key to continue . . .

 

The Debug Error message box will be invoked, click the Retry button.

 

 

C and C++ Programming Resources | C & C++ Code Example Index