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的更多相关文章

  1. 模板学习实践三 functor

    #include <iostream>#include <typeinfo> void foo(){ std::cout << "foo() called ...

  2. 《Hadoop学习之路》学习实践二——配置idea远程调试hadoop

    背景:在上篇文章中按照大神“扎心了老铁”的博客,在服务器上搭建了hadoop的伪分布式环境.大神的博客上是使用eclipse来调试,但是我入门以来一直用的是idea,eclipse已经不习惯,于是便摸 ...

  3. 模板学习实践一 accumulationtraits

    // 11111.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include &l ...

  4. 【前端,干货】react and redux教程学习实践(二)。

    前言 这篇博文接 [前端]react and redux教程学习实践,浅显易懂的实践学习方法. ,上一篇简略的做了一个redux的初级demo,今天深入的学习了一些新的.有用的,可以在生产项目中使用的 ...

  5. Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客

    ==他的博客应该不错,没有细看 Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客 http://blog.csdn.net/u012706811/article/det ...

  6. Appium学习实践(二)Python简单脚本以及元素的属性设置

    1.简单的Python脚本 Appium中的设置与Appium学习实践(一)简易运行Appium中的一致 Launch后,执行脚本 #coding:utf-8 import unittest impo ...

  7. linux内核分析实践二学习笔记

    Linux实践二--内核模块的编译 标签(空格分隔): 20135328陈都 理解内核的作用 Linux内核[kernel]是整个操作系统的最底层,它负责整个硬件的驱动,以及提供各种系统所需的核心功能 ...

  8. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第四天】

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

  9. Nagios学习实践系列——基本安装篇

    开篇介绍 最近由于工作需要,学习研究了一下Nagios的安装.配置.使用,关于Nagios的介绍,可以参考我上篇随笔Nagios学习实践系列——产品介绍篇 实验环境 操作系统:Red Hat Ente ...

随机推荐

  1. [蓝桥杯]ALGO-20.算法训练_求先序排列

    问题描述 给出一棵二叉树的中序与后序排列.求出它的先序排列.(约定树结点用不同的大写字母表示,长度<=). 输入格式 两行,每行一个字符串,分别表示中序和后序排列 输出格式 一个字符串,表示所求 ...

  2. js鼠标拖动(转载)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. Qt中三种解析xml的方式

    在下面的随笔中,我会根据xml的结构,给出Qt中解析这个xml的三种方式的代码.虽然,这个代码时通过调用Qt的函数实现的,但是,很多开源的C++解析xml的库,甚至很多其他语言解析xml的库,都和下面 ...

  4. java基础-反射(细节)

    java面试题--java反射机制? Java反射机制的作用:1)在运行时判断任意一个对象所属的类.2)在运行时判断任意一个类所具有的成员变量和方法.3)在运行时任意调用一个对象的方法4)在运行时构造 ...

  5. MAC地址表、ARP缓存表以及路由表

    一:MAC地址表详解 说到MAC地址表,就不得不说一下交换机的工作原理了,因为交换机是根据MAC地址表转发数据帧的.在交换机中有一张记录着局域网主机MAC地址与交换机接口的对应关系的表,交换机就是根据 ...

  6. 199. Spring Boot JNDI:这是虾米?

      [视频&交流平台] àSpringBoot视频:http://t.cn/R3QepWG à SpringCloud视频:http://t.cn/R3QeRZc à Spring Boot源 ...

  7. python字符串前面的r/u/b的意义 (笔记)

    u/U:表示unicode字符串 : 不是仅仅是针对中文, 可以针对任何的字符串,代表是对字符串进行unicode编码. r/R:非转义的原始字符串: 与普通字符相比,其他相对特殊的字符,其中可能包含 ...

  8. ROS--导航、路径规划和SLAM

    一.用move_base导航走正方形 1. roscore 2.执行 roslaunch rbx1_bringup fake_turtlebot.launch 然后 roslaunch rbx1_na ...

  9. 面向的phthon2+3 的场景,Anaconda 安装+环境配置+管理

    standard procedure in pyCharm for creating environment when Anaconda installed Create a conda env vi ...

  10. py库: arrow (时间)

    arrow是个时间日期库,简洁易用.支持python3.6 https://arrow.readthedocs.io/en/latest/ arrow官网api https://github.com/ ...