冲冲冲!!! ε=ε=ε=( ̄▽ ̄)
1.隐式类型转换
隐式类型转换 自动类型转换
C++中,int* ----> void* 隐式类型转换
void* ---> int* 不能隐式类型转换
基础数据类型之间都可以进行隐式类型转换
#include <iostream>
using namespace std;
void fun(void *pa){
cout << "yes" << endl;
}
void fun1(int *pa){
cout << "yes1" << endl;
}
int main(){
//int* pa = new int;
double* pa = new double;
fun(pa);
delete pa;
cout << "---------------" << endl;
void *p = NULL;
fun1(p);
return 0;
}
报错:
02.cpp:18:10: error: invalid conversion from 'void*' to 'int*' [-fpermissive] fun1(p);
2.强制类型转换
强制类型转换 (int)3.14 (目标类型)(源对象)
3.显示类型转换
1).静态类型转换
static_cast<目标类型>(源对象)
转换成功的前提是目标类型和源对象类之间,某一方能够隐式转换成另一方,
如果能进行隐式转换,则就可以进行静态转换.
#include <iostream>
using namespace std;
void fun(void *pa){
cout << "yes" << endl;
}
void fun1(int *pa){
cout << "yes1" << endl;
}
int main(){
//int* pa = new int;
double* pa = new double;
fun(pa);
delete pa;
cout << "---------------" << endl;
void *p = NULL;
fun1(static_cast<int*>(p));
return 0;
}
运行结果:
yes
---------------
yes1
2).去常属性类型转换
const_cast<目标类型>(源类型)
切记只能用与指针和引用去除const
int main(){
int* pa = new int;
//double* pa = new double;
fun(pa);
delete pa;
cout << "---------------" << endl;
void *p = NULL;
fun1(static_cast<int*>(p));
cout << "----------------" << endl;
const int *pc = new int(30);
//int *pd = pc; //error: invalid conversion from 'const int*' to 'int*' [-fpermissive]
int *pd = const_cast<int *>(pc);
return 0;
}
3).动态类型转换
dynamic_cast<目标类型>(源对象)
用于多态中父子类之间的多态转换.
用在具有多态性的父子类指针或引用之间
#include<iostream>
class Animal {
public:
virtual void cry() = 0;
};
class Dog : public Animal
{
public:
virtual void cry()
{
std::cout << "旺旺" << std::endl;
}
void dohome()
{
std::cout << "看家" << std::endl;
}
};
class Cat : public Animal
{
public:
virtual void cry()
{
std::cout << "喵喵" << std::endl;
}
void dohome()
{
std::cout << "抓老鼠" << std::endl;
}
};
int main()
{
Animal* base = NULL;
base = new Cat();
base->cry();
//⽤用于将⽗父类指针转换成⼦子类,
Dog *pDog = dynamic_cast<Dog*>(base); //此时转换时失败的,因为父类指针现在指向的对象是猫,所以转换狗是失败的。
//转换失败返回空(NULL)
if (pDog != NULL)
{
pDog->cry();
pDog->dohome();
}
Cat* pCat = dynamic_cast<Cat*>(base); //此时转换成功,成功将父类指针转换成子类指针
if (pCat != NULL)
{
pCat->cry();
pCat->dohome();
}
delete base;
base = new Dog();
//⽤用于将⽗父类指针转换成⼦子类,
pDog = dynamic_cast<Dog*>(base);
//转换失败返回空(NULL)
if (pDog != NULL)
{
pDog->cry();
pDog->dohome();
}
delete base;
// system("pause");
return 0;
}
运行结果:
[Running] cd "e:\vscode\代码\" && g++ dynam_cast.cpp -o dynam_cast && "e:\vscode\代码\"dynam_cast
喵喵
抓老鼠
旺旺
看家
4).重解释类型转换
reinterpret_cast<目标类型>(源对象)
用在指针与指针之间 或者 指针与整数之间 做类型转换
reinterpret_cast 常用的一个用途是转换函数指针类型,即可以将一种类型的函数指针转换为另一种类型的函数 指针,
但这种转换可能会导致不正确的结果。
总之,reinterpret_cast只用于底层代码,一般我们都用不到它
,如果你的代码中使用到这种转型,务必明白自己在干什么
#include <iostream>
using namespace std;
class A{
public:
int x;
};
class B{
public:
int y;
};
class C:public A,public B{
public:
int z;
};
int main(){
C c;
cout << &c << endl;
A* pa = &c;
B* pb = &c;//会根据其类子对象在子类对象中的位置进行调整
C* pc = &c;
cout << "-------------" << endl;
cout << pa << endl;
cout << pb << endl;
cout << pc << endl;
cout << "-------------" << endl;
pc = (C*)pb;
cout << pc << endl;
pc = static_cast<C*>(pb);//会根据基类子对象进行调整
cout << pc << endl;
pc = reinterpret_cast<C*>(pb);
cout << pc << endl;
return 0;
}