模板学习实践二 pointer
c++ template学习记录
使用模板将实际类型的指针进行封装
当变量退出作用域 自动delete
// 1111.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h" template <typename T>
class Holder {
private:
T* ptr; // refers to the object it holds (if any) public:
// default constructor: let the holder refer to nothing
Holder() : ptr(0) {
} // constructor for a pointer: let the holder refer to where the pointer refers
explicit Holder(T* p) : ptr(p) {
} // destructor: releases the object to which it refers (if any)
~Holder() {
delete ptr;
} // assignment of new pointer
Holder<T>& operator= (T* p) {
delete ptr;
ptr = p;
return *this;
} // pointer operators
T& operator* () const {
return *ptr;
} T* operator-> () const {
return ptr;
} // get referenced object (if any)
T* get() const {
return ptr;
} // release ownership of referenced object
void release() {
ptr = 0;
} // exchange ownership with other holder
void exchange_with(Holder<T>& h) {
std::swap(ptr, h.ptr);
} // exchange ownership with other pointer
void exchange_with(T*& p) {
std::swap(ptr, p);
} private:
// no copying and copy assignment allowed
Holder(Holder<T> const&);
Holder<T>& operator= (Holder<T> const&);
}; class Something {
public:
void perform() const {
}
}; void do_two_things()
{
Holder<Something> first(new Something);
first->perform(); Holder<Something> second(new Something);
second->perform();
} int main()
{
do_two_things();
}
// 1111111.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <stddef.h>
#include <iostream>
#include <vector> using namespace std; size_t* alloc_counter()
{
return ::new size_t;
} void dealloc_counter(size_t* ptr)
{
::delete ptr;
} class SimpleReferenceCount {
private:
size_t* counter; // the allocated counter
public:
SimpleReferenceCount() {
counter = NULL;
} // default copy constructor and copy-assignment operator
// are fine in that they just copy the shared counter public:
// allocate the counter and initialize its value to one:
template<typename T> void init(T*) {
counter = alloc_counter();
*counter = 1;
} // dispose of the counter:
template<typename T> void dispose(T*) {
dealloc_counter(counter);
} // increment by one:
template<typename T> void increment(T*) {
++*counter;
} // decrement by one:
template<typename T> void decrement(T*) {
--*counter;
} // test for zero:
template<typename T> bool is_zero(T*) {
return *counter == 0;
}
}; class StandardArrayPolicy {
public:
template<typename T> void dispose(T* array) {
delete[] array;
}
}; class StandardObjectPolicy {
public:
template<typename T> void dispose(T* object) {
delete object;
}
}; template<typename T,
typename CounterPolicy = SimpleReferenceCount,
typename ObjectPolicy = StandardObjectPolicy>
class CountingPtr : private CounterPolicy, private ObjectPolicy {
private:
// shortcuts:
typedef CounterPolicy CP;
typedef ObjectPolicy OP; T* object_pointed_to; // the object referred to (or NULL if none) public:
// default constructor (no explicit initialization):
CountingPtr() {
this->object_pointed_to = NULL;
} // a converting constructor (from a built-in pointer):
explicit CountingPtr(T* p) {
this->init(p); // init with ordinary pointer
} // copy constructor:
CountingPtr(CountingPtr<T, CP, OP> const& cp)
: CP((CP const&)cp), // copy policies
OP((OP const&)cp) {
this->attach(cp); // copy pointer and increment counter
} // destructor:
~CountingPtr() {
this->detach(); // decrement counter
// (and dispose counter if last owner)
} // assignment of a built-in pointer
CountingPtr<T, CP, OP>& operator= (T* p) {
// no counting pointer should point to *p yet:
assert(p != this->object_pointed_to);
this->detach(); // decrement counter
// (and dispose counter if last owner)
this->init(p); // init with ordinary pointer
return *this;
} // copy assignment (beware of self-assignment):
CountingPtr<T, CP, OP>&
operator= (CountingPtr<T, CP, OP> const& cp) {
if (this->object_pointed_to != cp.object_pointed_to) {
this->detach(); // decrement counter
// (and dispose counter if last owner)
CP::operator=((CP const&)cp); // assign policies
OP::operator=((OP const&)cp);
this->attach(cp); // copy pointer and increment counter
}
return *this;
} // the operators that make this a smart pointer:
T* operator-> () const {
return this->object_pointed_to;
} T& operator* () const {
return *this->object_pointed_to;
} // additional interfaces will be added later
//... private:
// helpers:
// - init with ordinary pointer (if any)
void init(T* p) {
if (p != NULL) {
CounterPolicy::init(p);
}
this->object_pointed_to = p;
} // - copy pointer and increment counter (if any)
void attach(CountingPtr<T, CP, OP> const& cp) {
this->object_pointed_to = cp.object_pointed_to;
if (cp.object_pointed_to != NULL) {
CounterPolicy::increment(cp.object_pointed_to);
}
} // - decrement counter (and dispose counter if last owner)
void detach() {
if (this->object_pointed_to != NULL) {
CounterPolicy::decrement(this->object_pointed_to);
if (CounterPolicy::is_zero(this->object_pointed_to)) {
// dispose counter, if necessary:
CounterPolicy::dispose(this->object_pointed_to);
// use object policy to dispose the object pointed to:
ObjectPolicy::dispose(this->object_pointed_to);
}
}
}
}; void test1()
{
std::cout << "\ntest1():\n";
CountingPtr<int> p0;
{
CountingPtr<int> p1(new int(42));
std::cout << "*p1: " << *p1 << std::endl; *p1 = 17;
std::cout << "*p1: " << *p1 << std::endl; CountingPtr<int> p2 = p1;
std::cout << "*p2: " << *p2 << std::endl; *p1 = 33;
std::cout << "*p2: " << *p2 << std::endl; p0 = p2;
std::cout << "*p0: " << *p0 << std::endl; ++*p0;
++*p1;
++*p2;
std::cout << "*p0: " << *p0 << std::endl;
std::cout << "*p1: " << *p1 << std::endl;
std::cout << "*p2: " << *p2 << std::endl;
}
std::cout << "after block: *p0: " << *p0 << std::endl;
} void test2()
{
std::cout << "\ntest2():\n";
{ CountingPtr<int> p0(new int(42));
CountingPtr<int> p2 = p0;
}
CountingPtr<int> p1(new int(42)); std::cout << "qqq" << std::endl; std::vector<CountingPtr<int> > coll;
std::cout << "qqq" << std::endl;
coll.push_back(p1);
std::cout << "qqq" << std::endl;
coll.push_back(p1);
std::cout << "qqq" << std::endl; std::cout << "qqq" << std::endl; ++*p1;
++*coll[0];
std::cout << *coll[1] << std::endl;
} int main()
{
test1();
test2();
}
模板学习实践二 pointer的更多相关文章
- 模板学习实践三 functor
#include <iostream>#include <typeinfo> void foo(){ std::cout << "foo() called ...
- 《Hadoop学习之路》学习实践二——配置idea远程调试hadoop
背景:在上篇文章中按照大神“扎心了老铁”的博客,在服务器上搭建了hadoop的伪分布式环境.大神的博客上是使用eclipse来调试,但是我入门以来一直用的是idea,eclipse已经不习惯,于是便摸 ...
- 模板学习实践一 accumulationtraits
// 11111.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include &l ...
- 【前端,干货】react and redux教程学习实践(二)。
前言 这篇博文接 [前端]react and redux教程学习实践,浅显易懂的实践学习方法. ,上一篇简略的做了一个redux的初级demo,今天深入的学习了一些新的.有用的,可以在生产项目中使用的 ...
- Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客
==他的博客应该不错,没有细看 Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客 http://blog.csdn.net/u012706811/article/det ...
- Appium学习实践(二)Python简单脚本以及元素的属性设置
1.简单的Python脚本 Appium中的设置与Appium学习实践(一)简易运行Appium中的一致 Launch后,执行脚本 #coding:utf-8 import unittest impo ...
- linux内核分析实践二学习笔记
Linux实践二--内核模块的编译 标签(空格分隔): 20135328陈都 理解内核的作用 Linux内核[kernel]是整个操作系统的最底层,它负责整个硬件的驱动,以及提供各种系统所需的核心功能 ...
- 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第四天】
https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...
- Nagios学习实践系列——基本安装篇
开篇介绍 最近由于工作需要,学习研究了一下Nagios的安装.配置.使用,关于Nagios的介绍,可以参考我上篇随笔Nagios学习实践系列——产品介绍篇 实验环境 操作系统:Red Hat Ente ...
随机推荐
- 记一次sql server 2005访问http接口,并解析json的过程
记一次sql server 2005访问http接口,并解析json的过程 JSON解析官方网站:https://www.red-gate.com/simple-talk/sql/t-sql-pro ...
- 将Long类型转为字母数字组合的jar包---Hashids
在设计数据库时,我有时喜欢使用自增Id,而不是uuid,但是在面对终端用户时,直接暴露id不是一个好的行为. 经过查询,可以使用 Hashids 这个jar包将id转为类似YouTube的大小写字母和 ...
- TCP/IP详解--TCP连接中TIME_WAIT状态过多
TIMEWAIT状态本身和应用层的客户端或者服务器是没有关系的.仅仅是主动关闭的一方,在使用FIN|ACK|FIN|ACK四分组正常关闭TCP连接的时候会出现这个TIMEWAIT.服务器在处理客户端请 ...
- Quartz入门及简单实现
Quartz简介 Quartz是一个功能丰富的开源作业调度库,可以集成到几乎任何Java应用程序中 - 从最小的独立应用程序到最大的电子商务系统.Quartz可以用来创建执行数十,数百乃至数万个作业的 ...
- pyton unittest
在说unittest之前,先说几个概念: TestCase 也就是测试用例 TestSuite 多个测试用例集合在一起,就是TestSuite TestLoader是用来加载TestCase到Test ...
- BZOJ 4872 luogu P3750 [六省联考2017]分手是祝愿
4872: [Shoi2017]分手是祝愿 Time Limit: 20 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description ...
- Android 开发 框架系列 Android-Universal-Image-Loader 图片加载使用demo
Android-Universal-Image-Loader github地址:https://github.com/nostra13/Android-Universal-Image-Loader 加 ...
- 今天看了几个小时的微信小程序说说心得体会
今天看了几个小时的微信小程序说说心得体会 小程序是个前端框架 根据微信相关提供了很多接口 1 先说说各种后缀的文件 .json 后缀的 JSON 配置文件.wxml 后缀的 WXML 模板文件.wxs ...
- Hive表种map字段的查询取用
建表可以用 map<string,string> 查询时可以按照 aaa[bbb], aaa 是map字段名,bbb是其中的参数名,就可以取到这个参数的值了 当参数名bbb是string时 ...
- python 正则表达式 RE模块汇总记录
re.compile(pattern, flags=0) re.search(pattern, string, flags=0) re.match(pattern, string, flags=0) ...