关键词:
#include <iostream>
#include <string>
using namespace std;
class Test
int i;
public:
Test(int i)
this->i = i;
int value()
return i;
~Test()
;
int main()
for(int i=0; i<5; i++)
Test* p = new Test(i);
cout << p->value() << endl;
return 0;
以上代码没有释放内存,会导致内存泄漏
#include <iostream>
#include <string>
using namespace std;
class Test
int i;
public:
Test(int i)
cout << "Test(int i)" << endl;
this->i = i;
int value()
return i;
~Test()
cout << "~Test()" << endl;
;
class Pointer
Test* mp;
public:
//智能指针很妙,关键在于构造函数,将Pointer类中的指针
//指向其他类中的指针所指向的空间
Pointer(Test* p = NULL)
mp = p;
Pointer(const Pointer& obj)
mp = obj.mp;
const_cast<Pointer&>(obj).mp = NULL;//剥夺对象只读属性
Pointer& operator = (const Pointer& obj)
if( this != &obj )
delete mp;
mp = obj.mp;
const_cast<Pointer&>(obj).mp = NULL;//剥夺对象只读属性
return *this;
Test* operator -> ()
return mp;
Test& operator * ()
return *mp;
bool isNull()
return (mp == NULL);
~Pointer()
delete mp;
;
int main()
Pointer p1 = new Test(0);
cout << p1->value() << endl;
Pointer p2 = p1;
cout << p1.isNull() << endl;
cout << p2->value() << endl;
return 0;
将Pointer指针对象进行比较会报错(对象之间不能比较)
智能指针只能指向堆空间,不能指向栈空间
小结
2021-08-26数组arraylistlist都能够存储一组对象,那么这三者到底有什么样的区别(代码片段)
在C#中数组、ArrayList、List都能够存储一组对象,那么这三者到底有什么样的区别呢?1.数组数组在C#中最早出现的。在内存中是连续存储的,所以它的索引速度非常快,而且赋值与修改元素也很简单。数组中某个指... 查看详情
2021-08-26:长度为n的数组arr,一定可以组成n^2个数字对。例如arr=[3,1,2],数字对有(3,3)(3,1)(3,2)(1,3)(1,1)(1,2)(2,3)(2(代码片段)
2021-08-26:长度为N的数组arr,一定可以组成N^2个数字对。例如arr=[3,1,2],数字对有(3,3)(3,1)(3,2)(1,3)(1,1)(1,2)(2,3)(2,1)(2,2),也就是任意两个数都可以,而且自己和自己也算数字对,数字对怎么排序?第一... 查看详情
2021-08-26
作为笔记本行业内的大佬,外星人的产品一直都受到无数游戏玩家的倾慕,而在如今的换机热潮中,“外星人游戏本哪一款好”这个问题,被广大网友“搬”上了谈判桌。实际上,在外星人众多笔记本产品中... 查看详情
将数组的Ruby数组转换为哈希
...英文标题】:ConvertingRubyarrayofarrayintoahash【发布时间】:2021-11-2516:40:03【问题描述】:我有一个数组如下:[["2021-07-26T11:38:42.000+09:00",1127167],["2021-08-26T11:38:42.000+09:00",1127170],["2021-09-26T11:38:42.000+09:00",112 查看详情
将嵌套的 JSON 对象展平并排序到 javascript 中的数组中
...FlattenandsortanestedJSONobjectintoanarrayinjavascript【发布时间】:2021-10-2621:12:45【问题描述】:我有一个类似的对象:\'2021\':\'08\':\'26\':[\'name\':\'Bob\',age:34,\'name\':\'Alice\',age:33],\'27\':[\' 查看详情
谷歌日历推送通知:未经授权的 webhook 调用
...lendarpushnotification:Unauthorizedwebhookcall【发布时间】:2017-07-2021:08:26【问题描述】:目前我们正在开发一款处理谷歌日历的iPhone应用程序。需要将日历事件同步到服务器端。一切正常。但是对于推送通知,我们关注thisprocess每当我们... 查看详情
markdowngit代码片段(代码片段)
csharp代码片段(代码片段)
javascript代码片段(代码片段)
textvisualbasic代码片段(代码片段)
sqloracle代码片段(代码片段)
swift代码片段(代码片段)
java代码片段【安卓】(代码片段)
shbash的代码片段(代码片段)
markdownphpexcelnotes和代码片段(代码片段)
javaandroid的代码片段(代码片段)
javascriptjs-常用代码片段(代码片段)
常用代码片段(代码片段)
单例模式privatestaticHttpUtilinstance;publicstaticsynchronizedHttpUtilgetInstance()if(instance==null)instance=newHttpUtil();returninstance; 查看详情