Casting class : Casting « Class « Visual C++ .NET

Home
Visual C++ .NET
1.2D
2.Class
3.Collections
4.Data Type
5.Database ADO.net
6.Delegate
7.Development
8.File Directory
9.Function
10.Generics
11.GUI Form
12.Language Basics
13.Network
14.Reflection
15.Security
16.Statement
17.Structure
18.Thread
19.XML
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Visual C++ .NET » Class » Casting 
Casting class
 
#include "stdafx.h"
using namespace System;

ref class {};
ref class B : public {};
ref class {};

void main()
{
    Object ^v1 = gcnew A();              
    Object ^v2 = gcnew B();           
    Object ^v3 = gcnew C();         

    A ^a1 = gcnew A();               
    A ^a2 = gcnew B();
    A ^a3 = dynamic_cast<A^>(v1);  // downcast
    A ^a4 = dynamic_cast<A^>(v2);  // downcast

    B ^b1 = gcnew B();
    B ^b2 = dynamic_cast<B^>(v2);  // downcast
    B ^b4 = dynamic_cast<B^>(a2);  // downcast
    
    C ^c1 = gcnew C();
    C ^c2 = dynamic_cast<C^>(v1);  // Fails c2 = null. Miss match classes
    C ^c3 = static_cast<C^>(v2);   // c3 has invalid value of type B class
    C ^c4 = safe_cast<C^>(v3);     // downcast

    C ^c5 = (C^)(v3);              // downcast


   
  
Related examples in the same category
1.Checking casts
2.Casting from object
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.