C++四种强制类型转换
2015-12-16 00:01
218 查看
大家都知道在C语言里强制类型转换的方法非常简单,只要在要转换的变量前用括号确定要转换的类型即可,如要将一个double型的变量强制转换为int类型,代码如下:
double x = 3.14;
int y = (int)x;
另外,还可有更高级的转换,如把int
*类型的变量转换为int ,代码如下:
int x =
20;
int *p = &x;
int y =
(int)p;
在C++语言里为了让强制类型转换更明显,更安全,所以把不同情况下的强制类型转换分为四种,分别为:
static_cast 静态类型转换,属于最安全的强制类型转换。
const_cast 在可变与不可变类型之间转换
dynamic_cast 在多态的情况下,父子类之间的转换
reinterpret_cast 所有类型间的转换,最不安全的转换
为了更好的说明问题,下面举几个使用方面的具体例子,代码如下:
static_cast
--------------------------------------------------------------------
int
main()
{
int x = 0;
double y =
3.14;
x =
static_cast<int>(y);
system("pause");
return
0;
}
--------------------------------------------------------------------
const_cast
--------------------------------------------------------------------
int
main()
{
int x = 20;
int const *p
= &x;
int *q = const_cast<int
*>(p);
system("pause");
return
0;
}
--------------------------------------------------------------------
dynamic_cast
--------------------------------------------------------------------
#include
<iostream>
using std::cout;
using std::endl;
//X类是父类
class
X
{
public:
void test()
{
cout << "Xtest" <<
endl;
}
//虚函数
virtual void foo()
{
cout << "XFoo" <<
endl;
}
};
//Y类是子类,继承了X类
class Y : public
X
{
public :
void TTT()
{
cout << "YTTT" <<
endl;
}
//重写的虚函数
virtual void foo()
{
cout << "YFoo" <<
endl;
}
};
//
int
main()
{
X *p = new Y;
dynamic_cast<Y
*>(p)->TTT();
system("pause");
return
0;
}
--------------------------------------------------------------------
reinterpret_cast
--------------------------------------------------------------------
int
main()
{
int x = 20;
int *p =
&x;
int z =
reinterpret_cast<int>(p);
system("pause");
return
0;
}
--------------------------------------------------------------------
double x = 3.14;
int y = (int)x;
另外,还可有更高级的转换,如把int
*类型的变量转换为int ,代码如下:
int x =
20;
int *p = &x;
int y =
(int)p;
在C++语言里为了让强制类型转换更明显,更安全,所以把不同情况下的强制类型转换分为四种,分别为:
static_cast 静态类型转换,属于最安全的强制类型转换。
const_cast 在可变与不可变类型之间转换
dynamic_cast 在多态的情况下,父子类之间的转换
reinterpret_cast 所有类型间的转换,最不安全的转换
为了更好的说明问题,下面举几个使用方面的具体例子,代码如下:
static_cast
--------------------------------------------------------------------
int
main()
{
int x = 0;
double y =
3.14;
x =
static_cast<int>(y);
system("pause");
return
0;
}
--------------------------------------------------------------------
const_cast
--------------------------------------------------------------------
int
main()
{
int x = 20;
int const *p
= &x;
int *q = const_cast<int
*>(p);
system("pause");
return
0;
}
--------------------------------------------------------------------
dynamic_cast
--------------------------------------------------------------------
#include
<iostream>
using std::cout;
using std::endl;
//X类是父类
class
X
{
public:
void test()
{
cout << "Xtest" <<
endl;
}
//虚函数
virtual void foo()
{
cout << "XFoo" <<
endl;
}
};
//Y类是子类,继承了X类
class Y : public
X
{
public :
void TTT()
{
cout << "YTTT" <<
endl;
}
//重写的虚函数
virtual void foo()
{
cout << "YFoo" <<
endl;
}
};
//
int
main()
{
X *p = new Y;
dynamic_cast<Y
*>(p)->TTT();
system("pause");
return
0;
}
--------------------------------------------------------------------
reinterpret_cast
--------------------------------------------------------------------
int
main()
{
int x = 20;
int *p =
&x;
int z =
reinterpret_cast<int>(p);
system("pause");
return
0;
}
--------------------------------------------------------------------
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 如何组织构建多文件 C 语言程序(二)
- 关于指针的一些事情
- 如何写好 C main 函数
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua和C语言的交互详解
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- 关于C语言中参数的传值问题
- 简要对比C语言中三个用于退出进程的函数
- 深入C++中API的问题详解
- 基于C语言string函数的详解
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C语言中fchdir()函数和rewinddir()函数的使用详解
- C语言内存对齐实例详解
- C++变位词问题分析