Checking casts : Casting « Class « Visual C++ .NET






Checking casts

 
#include "stdafx.h"
using namespace System;

ref class Base {};
ref class Derived : Base {} ;

int main(){

    Base^ b = gcnew Base();
    Derived^ d = gcnew Derived();
    try
    {
       d = safe_cast<Derived^>(b);
    }
    catch (InvalidCastException^ e)
    {
    }
    
    d = dynamic_cast<Derived^>(b);
    if (d == nullptr)
    {
    }
}

   
  








Related examples in the same category

1.Casting class
2.Casting from object