Casting in C++.


  • What is Casting in the view point of Class ?
Casting is the process of accessing methods of a class with a pointer to another class within the same class hierarchy.
  1. In a class hierarchy, a pointer to the base class can be used to call the implementation of base class virtual
    methods residing in the derived class object
    . Since the derived class contains the definitions of all the base
    classes from which it is derived, it is safe
    to cast a pointer up in the hierarchy to any of the parent (or base)
    classes.
    This is called upcasting because the pointer is moved up in the class hierarchy.
  2. Upcasting is usually typesafe, thus the base classes are subsets within the vtable of the derived class.
  3. If we try to access the derived class methods using a pointer to one of its parent (or base) class objects, we
    are downcasting the pointer because we are moving the pointer
    down in the class hierarchy. Since
    downcasting involves accessing methods
    not defined in the base class vtable, the object we are trying to ca
    st should
    be of the correct type for the casting to be safe.
  • C++ provides different casting operators for different situations.
Dynamic Casting
  • Operator is used to perform the upcasting and downcasting within the hierarchy.
  • Perform the run-time check to make the operation safe.
  • Perform on the ambiguous conversion and it will fail by returning of NULL in case of pointer cast or by throwing a bad_cast exception in case of reference cast.
  • dynamic_cast< T >(experssion);
Static Casting
  • Operator is Used for Non-Polymorphic type conversion, such as enum to int, int to float, and so on.
  • Static Casting doesn't perform a run-time checking to ensure safe casting.
  • Operator is used in situation where you are certain that the expression of casting is really an object of the appropriate type.
  • Operator is used in situation where you are certain the the conversion is not going to fail.
  • If conversion fail, it will just return, as if nothing went wrong.
static_cast< T >(expression); Const Casting
  • Operator must be used to add or remove the const and the volatile from identifiers.
  • Example:
    Const Casting Example
     1// sample code to show how the const_cast operator can be used
     2void func() {
     3// In the following example the const variable x is converted to
     4// a non-const variable y. The variable y can be modified in the
     5// program, just as any other int variable
     6const int x = 10;
     7int y = const_cast<int>(x);
     8
     9// In the following example the non-const variable a is converted to
    10// a const variable b. Now the variable b cannot be modified since it is a const.
    11int a = 10;
    12const int b = const_cast<const int>(a);
    13}

    1// sample code to show how the const_cast operator can be used
    2void func() {
    3// In the following example the const variable x is converted to
    4// a non-const variable y. The variable y can be modified in the
    5// program, just as any other int variable
    6const int x = 10;
    7int y = const_cast<int>(x);
    8// In the following example the non-const variable a is converted to
    9// a const variable b. Now the variable b cannot be modified since it is a const.
Reinterpret Casting

转载于:https://www.cnblogs.com/morrispan/archive/2008/02/04/1064221.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值