模板学习实践二 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 ...
随机推荐
- Beautiful Soup库基础用法(爬虫)
初识Beautiful Soup 官方文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc/# 中文文档:https://www.crumm ...
- Java多态(非常重要)
多态(多种形态)的定义 同一消息对不同类的对象做出的不同响应 种类 在程序设计中一般说多态都是运行时多态 多态施行的条件: 1满足继承关系 2父类引用指向子类对象(向上转型) 向上转型 向下转型(子类 ...
- ionic3安卓平台引用高德地图
1.前置条件 第一步,注册高德开发者:第二步,去控制台创建应用:第三步,获取Key. 2.打开src目录下的index.html, 在head标签中添加以下代码,引入js: <script ty ...
- 协议并发测试工具 BoHexTest
BoHexTest V1.0.3 1.添加连接LOG打印2.优化代理及并发策略 大小: 1074688 字节修改时间: 2017年10月3日, 10:24:26MD5: EBAE5A17F7F5ED0 ...
- Python的Django
1 第一部分目录详解 修改django的项目当中的url中的配置: from django.contrib import admin from django.conf.urls import ur ...
- wepy 小程序云开发
小程序前段时间更新了云开发的功能,刚听到对时候觉得简直是个人开发者对福音,尤其是对我这样对后端不是很懂对前端傻傻,简直不能太方便,就试了下. 体验二维码: 源码:https://github.com/ ...
- 深入理解Tomcat
简介 tomcat是一个web服务器,运行jsp和servlet,使用HTTP与客户端(通常是浏览器)进行通信. 构成 下图是tomcat的架构,可以看出:核心内容是Connector和Contain ...
- linux环境下载和安装scala
Linux下安装Scala和Windows下安装类似,步骤如下: 1.首先访问下载链接:http://www.scala-lang.org/download/默认这里下载的是Windows版本,这时点 ...
- 面试回顾——List<T>排序
1.如何对List<T>排序: public static void main(String[] args) { Student stu1=new Student("张三&quo ...
- Swift 通过运行时获取属性名列表
import UIKit //必须要有@objcMembers修饰符,否则获取到的成员属性为0 @objcMembers class Person: NSObject { var name: Stri ...