37)智能指针(就是自动delete空间)

1)问题引入:

      在java或者在C++中,一旦你new一个东西,那么必然有一个delete与之对应,比如:

 1 int main()
 2 {
 3      int* p= new int();
 4      
 5      *p=10 6 
 7       delete p;   
 8 
 9 
10      return 011 }

   但是  我现在有一个  特别  特别智能的指针   ---名字叫  智能指针  可以  不用我们手动释放  去自己释放,那么怎么创建和使用这个智能指针呢?

 

其实这个智能指针就类似于  JAVA的  new原理,也是你只要创建了,就可以用,但是回收 你不用管。

 

2)智能指针的创建和使用

    (1)智能指针存在头文件  <mrmary>  中

    (2)创建和使用:

 1 #include<iostream>
 2 #include<memory>
 3 using namespace std;
 4 int main()
 5 {
 6     //等同于 int *p= new int();
 7     auto_ptr<int> pt(new int);
 8     //这个pt后面括号里面的是 返回一个地址的东西   new int 返回一个地址  给了pt
 9     //那个尖括号< > 里面的是 这个pt指针的数据类型 你是int  就是<int> 你是char  
10     //就是  <char>  你是类A  就是 <A>
11 
12 
13     //比如  和 A  a=  new  A(10)类似的代码是:
14 
15     // auto_ptr<A>  pt(new A(10));
16     *pt=10;
17     cout<<*pt<<endl;
18 
19 
20 }

 

 

转载于:https://www.cnblogs.com/xiaoyoucai/p/8259597.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390377.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

linux 安装maven

2019独角兽企业重金招聘Python工程师标准>>> 目录:/usr/local/maven 1.下载 wget http://mirrors.hust.edu.cn/apache/maven/maven-3/3.5.3/binaries/apache-maven-3.5.3-bin.tar.gz 2.解压 tar -zxvf apache-maven-3.5.3-bin.tar.gz 3.配置 vi /etc/profile #讲下面…

自由开发者怎么生存_如何作为自由开发者生存

自由开发者怎么生存It’s been 8 weeks since we started experiencing the dramatic impact of the COVID-19 pandemic. In that time, we’ve all borne witness to how this virus can impact our families, our communities, and our livelihood. 自我们开始体验COVID-19大…

UUID生成字符串

在向数据库插入新数据时&#xff0c;可能需要插入字符串形式的ID&#xff0c;这时使用UUID可以生成随机字符串&#xff1a; String str UUID.randomUUID().toString(); 转载于:https://www.cnblogs.com/suhfj-825/p/8260861.html

如何在React Native中使用react-navigation 5处理导航

React-navigation is the navigation library that comes to my mind when we talk about navigation in React Native. 当我们谈论React Native中的导航时&#xff0c;React-navigation是我想到的导航库。 Im a big fan of this library and its always the first solution I…

flask内置session原理

内置session原理 请求到来 当请求进来之后&#xff0c;先执行Flask对象的 __call__ 方法 def wsgi_app(self, environ, start_response):# 获取请求相关数据&#xff0c;并进行封装和加工ctx self.request_context(environ)# 将请求消息推送到堆栈中&#xff0c;并执行 open_s…

指针3

#include <stdio.h>/* 2018-05-28 如何通过被调函数修改主调函数普通变量的值1&#xff0c;实参必须为该普通变量的地址2,形参必须为指针变量3&#xff0c;在背调函数中通过*形参名 。。。。。的方式就可以修改主调函数相关变量的值*/f(int *i,int *j) {*i 4;*j 5;ret…

面试系统设计_系统设计面试问题–您应该知道的概念

面试系统设计You may have heard the terms "Architecture" or "System Design." These come up a lot during developer job interviews – especially at big tech companies.您可能已经听说过“架构”或“系统设计”这两个术语。 在开发人员工作面试中&…

8597 石子划分问题 dpdp,只考虑第一次即可

8597 石子划分问题 时间限制:500MS 内存限制:1000K提交次数:155 通过次数:53 题型: 编程题 语言: G;GCC;VC Description 给定n个石子&#xff0c;其重量分别为a1,a2,a3,...,an。 要求将其划分为m份&#xff0c;每一份的划分费用定义为这份石子中最大重量与最小重量差的平方。…

文章中嵌入代码块_如何在您的文章中嵌入多项选择测验问题

文章中嵌入代码块In my experience, supplementing study with practical exercises greatly improves my understanding of a topic. This is especially true when I can test my knowledge as I go and receive instant feedback for each question.以我的经验&#xff0c;通…

mysql免安装版配置

1.官网下载https://dev.mysql.com/downloads/mysql/ 2.将下载好的压缩包mysql-5.7.20-winx64.zip解压。 3.mysql解压后&#xff0c;设置.ini文件&#xff0c;在加压后的路径中加一个my.ini文件 配置如下内容&#xff1a; # 设置mysql客户端默认字符集 default-character-setutf…

各种IE(IE6-IE10)兼容问题一行代码搞定

x-ua-compatible 用来指定IE浏览器解析编译页面的model x-ua-compatible 头标签大小写不敏感&#xff0c;必须用在 head 中&#xff0c;必须在除 title 外的其他 meta 之前使用。 1、使用一行代码来指定浏览器使用特定的文档模式。 <meta http-equiv"x-ua-compatible&q…

802. 找到最终的安全状态

在有向图中&#xff0c;以某个节点为起始节点&#xff0c;从该点出发&#xff0c;每一步沿着图中的一条有向边行走。如果到达的节点是终点&#xff08;即它没有连出的有向边&#xff09;&#xff0c;则停止。 对于一个起始节点&#xff0c;如果从该节点出发&#xff0c;无论每…

元类型与类型的区别

元类型是指所有类型的类型。 元类型只能类型出现在类型标示位&#xff1b; 类型即能作为类型存在&#xff0c;出现在类型标示位&#xff1b; 也能作为变量存在&#xff0c;出现在元类型的变量位。 http://www.swift51.com/swift2.0/chapter3/03_Types.html#type_inheritance_cl…

css 动画使用_如何在CSS中使用动画

css 动画使用使用CSS动画 (Using CSS Animations) CSS animations add beauty to the webpages and make transitions from one CSS style to the other beautiful.CSS动画可以使网页更加美观&#xff0c;并可以从一种CSS样式过渡到另一种CSS样式。 To create a CSS animation…

第01章—快速构建

spring boot 系列学习记录&#xff1a;http://www.cnblogs.com/jinxiaohang/p/8111057.html 码云源码地址&#xff1a;https://gitee.com/jinxiaohang/springboot 一、Spring Initializr 使用教程 &#xff08;IntelliJ IDEA&#xff09; 具体步骤&#xff1a; 1、打开IDEA &am…

看板和scrum_看板VS Scrum-如何变得敏捷

看板和scrumScrum and Kanban are the two most popular project management techniques today in business. As a developer, I think its important to understand these processes as you will likely be heavily involved in them if you are part of a team. By understan…

JS之Promise

开胃菜&#xff0c;先做如下思考&#xff1a; Promise 有几种状态&#xff1f;Promise 状态之间可以转化吗&#xff1f;Promise 中的异常可以被 try...catch 捕获吗&#xff1f;Promise前世 callback hell 大家都知道JS是异步操作&#xff08;执行&#xff09;的&#xff0c;在…

鱼眼镜头的distortion校正【matlab】

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 作者&#xff1a;WWC %%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% 功能&#xff1a;畸变矫正 clc; clear; close all; %% 读取图像 Aimread(D:\文件及下载相关\图片\distortion2.jpg)…

web后端开发学习路线_学习后端Web开发的最佳方法

web后端开发学习路线My previous article described how you can get into frontend development. It also discussed how the front end can be a place filled with landmines – step in the wrong place and youll be overwhelmed by the many frameworks of the JavaScrip…

C# 使用WinApi操作剪切板Clipboard

前言&#xff1a; 最近正好写一个程序&#xff0c;需要操作剪切板 功能很简单&#xff0c;只需要从剪切板内读取字符串&#xff0c;然后清空剪切板&#xff0c;然后再把字符串导入剪切板 我想当然的使用我最拿手的C#来完成这项工作&#xff0c;原因无他&#xff0c;因为.Net框架…