c++中的智能指针与类型转换

文章介绍了C++中的智能指针,如auto_ptr和unique_ptr,作为解决内存泄漏问题的工具,以及四种类型转换函数:dynamic_cast、const_cast、static_cast和reinterpret_cast的基本用法。同时,文章提供了相关示例代码以帮助理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1.智能指针

 2.类型转换


 

1.智能指针

#include<memory>
   1.作用
                int *p=new int;   //堆空间申请以后,使用完毕记得释放,如果程序员一直申请,从来都没有释放,会导致内存泄漏(没有多余的堆空间)
                为了解决程序员容易忘记释放堆空间的毛病,C++发明了智能指针
     原理
                所谓的智能指针,本质是个模板类,在类的构造函数中自动帮你申请堆空间,在类的析构函数中自动帮你释放堆空间,程序员不必操心堆空间的释放问题
                template<typename T>
                class  unique_ptr
                {
                     public:
                             unique_ptr(参数)
                             {
                                    point=new 参数;
                             } 
                              ~unique_ptr()
                             {
                                    delete 释放指针堆空间;
                             } 
                     private:
                             T *point;
                };

2.常用的智能指针
               老版本的C++(C++11之前)
                       auto_ptr<模板类型>  对象(实参);
                              auto_ptr<int> p(new int);
                  *p=666;
               C++11新增的智能指针
                       unique_ptr<模板类型>  对象();
                       unique_ptr<int> p(new int);  //分配一个int大小的堆空间  
                       unique_ptr<int[]> p(new int[10]);
                   p[0]=15;  //*p
                   p[1]=56; //*(p+1)
                   p[2]=89;
 

#include<iostream>
#include<memory>
using namespace std;

int main()
{
	//定义智能指针对象
	auto_ptr<int> p(new int);
	*p=666;
}

 2.类型转换

  1.以下四种都是模板函数
         dynamic_cast  //用于父类指针指向子类对象的转换(多态)(本来可以自动转换,不需要使用这个函数,但是规范写法还是写上)
                父类 *p=dynamic_cast<父类 *>(子类指针);
         const_cast    //用于把一个const修改的指针,赋值给一个非const修饰的指针
                    People people1;
         const People *p=&people1;
         //People *q=p; 编译语法错误
         People *q=const_cast<People *>(p); 
         static_cast  //子类指针指向父类的转换
                   static_cast<类型1>(类型2)  
         reinterpret_cast //强制转换,跟你的C语言(强制转换)一样
                   struct student  a={"张三",18};
                   struct student *p=&a;
       C语言            char *q=(char *)p;
       C++              char *q=reinterpret_cast<char *>(p);

 注意:   实现文件拷贝(如果拷贝大文件,需要循环读取,循环写入)
                   判断文件是否读取完毕--》eof()--》读取完毕返回true
                   gcount()方法干什么 --》返回read成功读取的字节数

#include<iostream>
using namespace std;

class Animal
{
	
};

class Cat:public Animal
{
	
};

int main()
{
	Cat c;
	Animal *p=dynamic_cast<Animal *>(&c);
}
#include<iostream>
using namespace std;

int main()
{
	string str="helloworld";
	
	//string类型的字符串转换成char*
	//char *p=str.c_str();  编译语法错误,c_str返回值是const char*,但是p不是const修饰的指针
	
	char *p=const_cast<char *>(str.c_str());
	
}
#include<iostream>
using namespace std;
class Animal
{
	
};

class Cat:public Animal
{
	
};
int main()
{
	Animal a;
	//子类指针指向父类
	Cat *p=static_cast<Cat *>(&a);
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hqb_newfarmer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值