c语言 cast operator

Syntax  ( type-name ) expression

n addition to the implicit conversions, the following conversions are allowed:

1、Any integer can be cast to any pointer type. Except for the null pointer constants such as NULL (which doesn't need a cast), the result is implementation-defined.

任何整型都可以被强制转换成指针类型。除了Null Point.强制转换结果由编译器定义。

2、Any pointer type can be cast to any integer type. The result is implementation-defined, even for null pointer values (they do not necessarily result in the value zero).

任何指针可以被转换成整型。强制转换结果由编译器实现。

3、Any pointer to object can be cast to any other pointer to object. If the value is not correctly aligned for the target type, the behavior is undefined.

任何指针类型对象可以转换成其它任何类型的指针对象。

 if the value is converted back to the original type, it compares equal to the original value.

如果,指针又通过强制转换成最初的对象,那边指针还是原来的数据。

If a pointer to object is cast to pointer to any character type, the result points at the lowest byte of the object and may be incremented up to sizeof the target type

若果指针被强制转换成 char类型(char,unsiged char siged char)指针,则char指针指向原来指针开始位置(lowest byte).

in other words, can be used to examine object representation or to make a copy via memcpy or memmove

4、Any pointer to function can be cast to a pointer to any other function type. If the resulting pointer is converted back to the original type, it compares equal to the original value.


任何函数对象的指针可以转换成其它任何函数的指针。转换的结果由编译器实现。

5、When casting between pointers (either object or function), if the original value is a null pointer value of its type, the result is the correct null pointer value for the target type.

可以把函数指针和指针之间相互强制转换。


    Object*  o1 = new Object();
    o1->o = 9;
    long l1 = (long)o1;

    cout << "point ->long:" << l1 << endl;

    Object* o2 = (Object*)l1;

    cout << "object.o:" << o2->o << endl;



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值