C++中的类型转换问题

本文深入探讨了C++中的类型转换,包括隐式类型转换、静态类型转换(static_cast)、常属性类型转换(const_cast)、动态类型转换(dynamic_cast)和重解释类型转换(reinterpret_cast)。通过示例代码展示了各种类型转换的使用场景和注意事项,特别是在多态和指针转换中的应用。
摘要由CSDN通过智能技术生成

冲冲冲!!! ε=ε=ε=( ̄▽ ̄)


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;	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值