模板学习实践二 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 ...
随机推荐
- Android批量验证渠道、版本号(windows版)
功能:可校验单个或目录下所有apk文件的渠道号.版本号,此为windows版,稍后整理Linux版使用说明:1.copy需要校验的apk文件到VerifyChannelVersion目录下2.双击运行 ...
- centos7如何安装部署Zabbix
参考http://www.cnblogs.com/momoshouhu/p/8041293.html 1.关闭selinux和firewall 1.1检测selinux是否关闭 [root@local ...
- google的protobuf简单介绍
google的protobuf是一种轻便高效的结构化数据存储格式,在通信协议和数据存储等领域中使用比较多.protobuf对于结构中的每个成员,会提供set系列函数和get系列函数. 但是,对于使用来 ...
- Scrapy学习篇(十二)之设置随机IP代理(IPProxy)
当我们需要大量的爬取网站信息时,除了切换User-Agent之外,另外一个重要的方式就是设置IP代理,以防止我们的爬虫被拒绝,下面我们就来演示scrapy如何设置随机IPProxy. 设置随机IPPr ...
- 关于CPU CACHE工作机制的学习
转自:http://blog.csdn.net/notbaron/article/details/48143409 1. 存储层次结构 由于两个不谋而合的因素如下: l 硬件:由于不同存储技术的访 ...
- You Only Look Once: Unified, Real-Time Object Detection
论文下载:http://arxiv.org/abs/1506.02640 代码下载:https://github.com/pjreddie/darknet Abstract 作者提出一种新的目标检测 ...
- solr搜索
安装过程: 原料:solr-4.10.3.tgz.tgz 1.1.1 安装步骤 单独一台虚拟机先全部删除:根目录:rm * -rf cd /usr/local \ rm ...
- python excel 像 Excel 一样使用 python 进行数据分析
https://www.jb51.net/article/63216.htm 像 Excel 一样使用 python 进行数据分析 : https://www.cnblogs.com/nxld/p/ ...
- leetcode124
class Solution { int maxValue; public int maxPathSum(TreeNode root) { maxValue = Integer.MIN_VALUE; ...
- 目标检测框架py-faster-rcnn修改anchor_box
众所周知,anchor_box控制了回归框的大小,我们有时候检测的是大物体或小物体时,需要调整回归框的大小的时候,得改一下anchor_box.基于rgb公开的py-faster-rcnn修改anch ...