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

目录

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);
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C++智能指针之间的类型转换可以通过以下几种方式实现: 1. 隐式转换:如果两个智能指针类型具有相同的底层指针类型,可以直接进行隐式转换。例如,可以将`shared_ptr`隐式转换为`unique_ptr`。 ```cpp std::shared_ptr<int> sharedPtr = std::make_shared<int>(10); std::unique_ptr<int> uniquePtr = sharedPtr; // 隐式转换 ``` 2. 显式转换:可以使用`static_pointer_cast`、`dynamic_pointer_cast`和`const_pointer_cast`等函数进行显式类型转换。 - `static_pointer_cast`用于在两个相关类型之间进行转换,不会进行运行时类型检查。 - `dynamic_pointer_cast`用于在两个相关类型之间进行转换,并进行运行时类型检查。如果转换失败,返回空智能指针。 - `const_pointer_cast`用于在智能指针之间进行const或volatile修饰符的转换。 ```cpp std::shared_ptr<Base> sharedPtr = std::make_shared<Derived>(); std::shared_ptr<Derived> derivedPtr = std::dynamic_pointer_cast<Derived>(sharedPtr); // 显式转换 ``` 3. 使用`reinterpret_pointer_cast`:如果需要进行底层指针的重新解释转换,可以使用`reinterpret_pointer_cast`函数。但请注意,这种方式是非标准的,并且可能导致未定义的行为。 ```cpp std::shared_ptr<int> sharedPtr = std::make_shared<int>(10); std::shared_ptr<float> floatPtr = std::reinterpret_pointer_cast<float>(sharedPtr); // 重新解释转换 ``` 需要注意的是,在进行智能指针类型转换时,请确保转换的正确性,以避免潜在的内存安全问题。同时,尽量避免使用`reinterpret_pointer_cast`,以确保代码的可移植性和安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

hqb_newfarmer

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

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

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

打赏作者

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

抵扣说明:

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

余额充值