static_cast < type-id > ( expression )与reinterpret_cast<type-id> (expression)的区别

static_cast< type-id > ( expression )与reinterpret_cast<type-id> (expression)的区别

1.    static_cast < type-id > ( expression )

该运算符把expression转换为type-id类型,但没有运行时类型检查来保证转换的安全性。它主要有如下几种用法:

①用于类层次结构中基类(父类)和派生类(子类)之间指针或引用的转换。

进行上行转换(把派生类的指针或引用转换成基类表示)是安全的;

进行下行转换(把基类指针或引用转换成派生类表示)时,由于没有动态类型检查,所以是不安全的。

②用于基本数据类型之间的转换,如把int转换成char,把int转换成enum。这种转换的安全性也要开发人员来保证。2.1
C语言中隐式类型转换的地方均可使用static_cast<>()进行转换

③把空指针转换成目标类型的空指针?。

例如:

double da = 1.1;

void *pa =&da;

double *dp =static_cast<double*>(pa);

double *ddp=
reinterpret_cast<double*>(pa);

int ia =
static_cast<int>(da);

cout << *dp << endl;

cout<<*ddp<<endl;

cout << ia << endl;

输出结果:

④把任何类型的表达式转换成void类型

例如char* pa=”abcd”;void * pb=static_cast<void*> pa;

2.      reinterpret_cast<type-id>(expression)

type-id 必须是一个指针、引用、算术类型、函数指针或者成员指针。它可以把一个指针转换成一个整数,也可以把一个整数转换成一个指针(先把一个指针转换成一个整数,再把该整数转换成原类型的指针,还可以得到原先的指针值)。

操作符修改了操作数类型,但仅仅是重新解释了给出的对象的比特模型而没有进行二进制转换

(1) Static_cast<>()
静态类型转换,编译时C++
编译器会做类型检查,基本类型能转换,但是不能转换指针类型。

(2)若不同类型之间,进行强制类型转换,用reinterpret<>()
进行重新解释

(3)一般性结论:C语言中能隐式类型转换的,在C++
中可用static_cast<>()进行类型转换。因为C++
编译器在编译检查一般都能通过

C语言中不能用隐式类型转换的,在C++中可以用reinterpret_cast<>()进行强制类型解释。

例如:

例1:

int *n=
new int ;

*n=100;

double *d=reinterpret_cast<double*>(n);

double dd=static_cast<double>(*n);

cout<<*n<<endl;

cout<<*d<<endl;

cout<<dd<<endl;

运行结果:

例2:

char* p1="abcd";

int *p2=reinterpret_cast<int*>(p1);

int *p5=(int*)p1;

cout<<p1<<endl;

cout<<p2<<endl;

cout<<p5<<endl;

运行结果:

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-28 04:17:39

static_cast < type-id > ( expression )与reinterpret_cast<type-id> (expression)的区别的相关文章

[java] javax.el.PropertyNotFoundException: Property &#39;id&#39; not found on type bean.Student

问题提出: 在使用MyEclipse开发Java Web时,调用DAO和Java Bean出现了如下错误: 严重: Servlet.service() for servlet [jsp] in context with path [/JDBCbyDao] threw exception [An exception occurred processing JSP page /student.jsp at line 37 34: 35: <c:forEach items="${ student

javax.el.PropertyNotFoundException: Property &#39;id&#39; not found on type java.lang.Integer

javax.el.PropertyNotFoundException: Property 'id' not found on type java.lang.Integer    at javax.el.BeanELResolver$BeanProperties.get(BeanELResolver.java:237)    at javax.el.BeanELResolver$BeanProperties.access$400(BeanELResolver.java:214)    at jav

iOS Assigning to &#39;id&lt;XXXDelegate&gt;&#39; from incompatible type &#39;BViewController *__strong&#39;

在使用代理的时候, BViewController *BVC = [[BViewController alloc]init]; self.delegate = BVC; 出现这样的警告Assigning to 'id<XXXDelegate>' from incompatible type 'BViewController *__strong' 解决方案: #import "BViewController.h" @interface BViewController ()&l

Property &#39;id&#39; not found on type java.lang.String问题解决

一般出现上述问题:存在两种情况 1.你的程序的属性没有提供getter方法 2.在<c:forEach var="list" items="${requestScope.list}" varStatus="vs">这里你没有采取EL表达式 但是我却犯了一个很弱智的错误 在这里items="${requestScope.list}"我写成了 items=" ${requestScope.list}"

Property &#39;id&#39; not found on type java.lang.String

改为 忘写了$符,取不出来,因此报错! Property 'id' not found on type java.lang.String

Provided id of the wrong type for class pojo.Books. Expected: class java.lang.Integer, got class java.lang.Long

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). log4j:WARN Please initialize the log4j system properly. Exception in thread "main" org.hibernate.TypeMismatchException: Provided id of the wrong type for class po

org.hibernate.TypeMismatchException: Provided id of the wrong type for class cn.itcast.entity.User. Expected: class java.lang.String, got class java.lang.Integer at org.hibernate.event.internal.Defau

出现org.hibernate.TypeMismatchException: Provided id of the wrong type for class cn.itcast.entity.User. Expected: class java.lang.String, got class java.lang.Integer at org.hibernate.event.internal.DefaultLoadEventListener.checkIdClass(DefaultLoadEvent

如何用jquery获取&lt;input id=&quot;test&quot; name=&quot;test&quot; type=&quot;text&quot;/&gt;中输入的值?

如何用jquery获取<input id="test" name="test" type="text"/>中输入的值?$(" #test ").val()$(" input[ name='test' ] ").val()$(" input[ type='text' ] ").val()$(" input[ type='text' ]").attr(&quo

(转载)Android xml资源文件中@、@android:type、@*、?、@+引用写法含义以及区别

原帖地址:http://blog.csdn.net/zfrong/article/details/7332545 Android xml资源文件中@.@android:type.@*.?.@+引用写法含义以及区别 一[email protected]代表引用资源 1.引用自定义资源.格式:@[package:]type/name android:text="@string/hello" 2.引用系统资源.格式:@android:type/name android:textColor=&