模板类之间的友元关系实现Blob和BlobPtr
16.12编写你自己版本的Blob和BlobPtr模板,包含书中未定义的多个const成员。
Blob.h(注意,成员函数的声明和定义要放在一个头文件中)
- /*记住,模板的头文件中通常既包括声明也包括定义。函数模板和类模板成员函数的定义通常放在头文件中,不能分开放。。。。谨记*/
- #ifndef BLOB_H
- #define BLOB_H
- #include<iostream>
- #include<vector>
- #include<string>
- #include<memory>
- #include<initializer_list>
- using namespace std;
- template <typename> class BlobPtr;
- template <typename> class Blob;
- template <typename T>
- bool operator==(const Blob<T>&,const Blob<T>&);
- template <typename T>
- class Blob
- {
- friend class BlobPtr<T>;
- friend bool operator==<T>
- (const Blob<T>&,const Blob<T>&);
- public:
- typedef T value_type;
- typedef typename vector<T>::size_type size_type;
- Blob();
- Blob(initializer_list<T> il);
- BlobPtr<T> begin() { return *this;}
- BlobPtr<T> end() { auto ret=BlobPtr<T>(*this,this->size()); return ret;}
- size_type size() const { return data->size();}
- bool empty() const { return data->empty();}
- void push_back(const T &t) { data->push_back(t);}
- void push_back(T &&t) { data->push_back(std::move(t));}
- void pop_back();
- T& front();
- T& back();
- const T& front() const;
- const T& back() const;
- T& operator[](size_type i);
- const T& operator[](size_type i) const;
- private:
- shared_ptr<vector<T>> data;
- void check(size_type i,const string &msg) const;
- };
- template <typename T>
- Blob<T>::Blob():
- data(std::make_shared<std::vector<T>>()) { }
- template<typename T>
- Blob<T>::Blob(initializer_list<T> il):data(make_shared<vector<T>>(il)) {}
- template<typename T>
- void Blob<T>::check(size_type i,const string &msg) const
- {
- if(i>=data->size())
- throw out_of_range(msg);
- }
- template<typename T>
- void Blob<T>::pop_back()
- {
- check(,"pop_back");
- data->pop_back();
- }
- template <typename T>
- T& Blob<T>::front()
- {
- check(,"front");
- return data->front();
- }
- template<typename T>
- T& Blob<T>::back()
- {
- check(,"back");
- return data->back();
- }
- template<typename T>
- const T& Blob<T>::front() const
- {
- check(,"front");
- return data->front();
- }
- template<typename T>
- const T& Blob<T>::back() const
- {
- check(,"back");
- return data->back();
- }
- template<typename T>
- T& Blob<T>::operator[](size_type i)
- {
- check(i,"out_of_range");
- return (*data)[i];
- }
- template<typename T>
- const T& Blob<T>::operator[](size_type i) const
- {
- check(i,"out_of_range");
- return (*data)[i];
- }
- template <typename T>
- bool operator==(const Blob<T> &lhs,const Blob<T> &rhs)
- {
- if(rhs.size()!=lhs.size())
- return false;
- for(size_t i=;i!=lhs.size();+i)
- if(lhs[i]!=rhs[i])
- return false;
- return true;
- }
- template <typename T>
- ostream operator<<(ostream &os,const Blob<T> &a)
- {
- os<<"<";
- for(size_t i=;i!=a.size();++i)
- os<<a[i]<<" ";
- os<<">";
- return os;
- }
- template <typename T>
- bool operator==(const BlobPtr<T>&,const BlobPtr<T>&);
- template <typename T> class BlobPtr
- {
- friend bool operator==<T>
- (const BlobPtr<T>&,const BlobPtr<T>&);
- public:
- BlobPtr():curr(){}
- BlobPtr(Blob<T> &a,size_t sz=):wptr(a.data),curr(sz){}
- T& operator*() const
- {
- auto p=check(curr,"dereference past end");
- return (*p)[curr];
- }
- BlobPtr& operator++();
- BlobPtr& operator--();
- BlobPtr& operator++(int);
- BlobPtr& operator--(int);
- private:
- shared_ptr<vector<T>> check(size_t,const string &) const;
- weak_ptr<vector<T>> wptr;
- size_t curr;
- };
- template <typename T>
- shared_ptr<vector<T>> BlobPtr<T>::check(size_t i,const string &msg) const
- {
- auto ret=wptr.lock();
- if(!ret)
- throw runtime_error("unbind BlobPtr");
- if(i>=ret->size())
- throw out_of_range(msg);
- return ret;
- }
- template <typename T>
- BlobPtr<T>& BlobPtr<T>::operator++()
- {
- check(curr,"++");
- ++curr;
- return *this;
- }
- template <typename T>
- BlobPtr<T>& BlobPtr<T>::operator--()
- {
- --curr;
- check(curr,"--");
- return *this;
- }
- template <typename T>
- BlobPtr<T>& BlobPtr<T>::operator++(int)
- {
- auto ret=*this;
- ++*this;
- return ret;
- }
- template <typename T>
- BlobPtr<T>& BlobPtr<T>::operator--(int)
- {
- auto ret=*this;
- --*this;
- return ret;
- }
- template <typename T>
- bool operator==(const BlobPtr<T> &lhs,const BlobPtr<T> &rhs)
- {
- return (lhs.wptr.lock().get()==rhs.wptr.lock().get())&&lhs.curr==rhs.curr;
- }
- template <typename T>
- bool operator!=(const BlobPtr<T> &lhs,const BlobPtr<T> &rhs)
- {
- return !(rhs==lhs);
- }
- #endif // BLOB_H
main.cpp
- #include "Blob.h"
- int main()
- {
- Blob<string> b1; // empty Blob
- cout << b1.size() << endl;
- { // new scope
- Blob<string> b2 = {"a", "an", "the"};
- b1 = b2; // b1 and b2 share the same elements
- b2.push_back("about");
- cout << b1.size() << " " << b2.size() << endl;
- } // b2 is destroyed, but the elements it points to must not be destroyed
- cout << b1.size() << endl;
- for(auto p = b1.begin(); p != b1.end(); ++p)
- cout << *p << endl;
- return ;
- }
16.14 编写Screen类模板,用非类型参数定义Screen的高和宽。
16.15为你的Screen模板实现输入和输出运算符。Screen类需要哪些友元来令输入和输出运算符正确工作。
- #include<iostream>
- #include <string>
- using namespace std;
- //模板非类型参数,友元一定要前置声明
- template <unsigned H,unsigned W>
- class Screen;
- template <unsigned H,unsigned W>
- ostream& operator<<(ostream &os,const Screen<H,W> &s);
- template <unsigned H,unsigned W>
- istream& operator>>(istream &is,Screen<H,W> &s);
- template <unsigned H,unsigned W>
- class Screen
- {
- friend ostream& operator<< <H,W> (ostream &os,const Screen<H,W> &s);
- friend istream& operator>> <H,W> (istream &is,Screen<H,W> &s);
- public:
- typedef string::size_type pos;
- Screen()=default;
- Screen(pos ht,pos wd,char c):height(ht),width(wd),contents(ht*wd,c){}
- char get() const
- {
- return contents[cursor];
- }
- inline char get(pos ht,pos wd) const;
- Screen &move(pos r,pos c);
- private:
- pos cursor=;
- pos height=H;
- pos width=W;
- string contents;
- };
- template<unsigned H,unsigned W>
- char Screen<H,W>::get(pos ht,pos wd) const
- {
- pos row=ht*width;
- return contents[row+wd];
- }
- template <unsigned H,unsigned W>
- Screen<H,W>& Screen<H,W>::move(pos r,pos c)
- {
- pos row=r*width;
- cursor=row+c;
- return *this;
- }
- template <unsigned H,unsigned W>
- ostream& operator<<(ostream &os,const Screen<H,W> &s)
- {
- os<<s.cursor<<" "<<s.height<<" "<<s.width<<" "<<s.contents[s.cursor]<<endl;
- return os;
- }
- template <unsigned H,unsigned W>
- istream& operator>>(istream &is,Screen<H,W> &s)
- {
- is>>s.cursor>>s.height>>s.width>>s.contents;
- return is;
- }
- int main()
- {}
16.16
模板类之间的友元关系实现Blob和BlobPtr的更多相关文章
- Hibernate中的Entity类之间的继承关系之一MappedSuperclass
在hibernate中,Entity类可以继承Entity类或非Entity类.但是,关系数据库表之间不存在继承的关系.那么在Entity类之间的继承关系,在数据库表中如何表示呢? Hibernate ...
- 06 (OC)* iOS中UI类之间的继承关系
iOS中UI类之间的继承关系 此图可以更好的让你去理解iOS中一些底层的关系.你能够了解以及理解UI类之间的继承关系,你会更加明白苹果有关于底层的东西,更有助于你的项目开发由它们的底层关系,就能更加容 ...
- JAVA类与类之间的全部关系简述+代码详解
本文转自: https://blog.csdn.net/wq6ylg08/article/details/81092056类和类之间关系包括了 is a,has a, use a三种关系(1)is a ...
- Java类与类之间的继承关系
Java父类与子类继承关系,调用的各种关系 示例一(子类调用父类函数): // 定义一类 A public class A { // 此方法打印一句话 public void a() { System ...
- Java学习笔记——I/O流常用类之间的继承关系及构造方法
朝辞白帝彩云间,千里江陵一日还. 两岸猿声啼不住,轻舟已过万重山. ——早发白帝城 总结一下有哪些I/O流: 输入流方法主要是read()和close(),输出流方法主要是write().flush( ...
- VS2015 查看类之间的继承关系
---恢复内容开始--- 1. 右击项目名称,单击"查看"菜单下的"查看类图"菜单: 2.生成的类图如下:
- python_面向对象——类之间的依赖关系
class Dog: def __init__(self,name,age,master): self.name = name self.age = age self.master = master ...
- 齐博X1模板页面之间的继承关系
本节说明下模板页面间的继承 我们在前面建立了一个公共布局模板,并且利用{block name=xxx}...{/block}分割了三个部分区块 本节我们来看下模板之前的继承如何实现,首先我们建立一个i ...
- eclipse如何查看类之间的引用关系
今天遇到这个问题:mark一点点: 在类名上单击右键.选择Reference->Workingspace快捷克债券Ctrl+Shift+G 版权声明:本文博客原创文章,博客,未经同意,不得转载.
随机推荐
- C语言可变参数在宏定义中的应用
在C语言的标准库中,printf.scanf.sscanf.sprintf.sscanf这些标准库的输入输出函数,参数都是可变的.在调试程序时,我们可能希望定义一个参数可变的输出函数来记录日志,那么用 ...
- eclipse+maven搭建cxf webservice 完整例子
开发环境是eclipse , maven. 在开发java webservice时,有两个比较流行的框架:axis2和cxf.cxf可以无缝的和spring集成,而axis2需要打包成aar文件,在t ...
- 使用VisualStudio2010创建C#应用程序
打开VisualStudio2010,选择“文件”——“新建”——“项目”菜单命令.调出“新建项目”对话框.
- 李洪强iOS开发Swift篇—03_字符串和数据类型
李洪强iOS开发Swift篇—03_字符串和数据类型 一.字符串 字符串是String类型的数据,用双引号""包住文字内容 let website = "http:// ...
- zip压缩解压缩 项目icsharpcode-SharpZipLib-e012155
大家可以到http://www.icsharpcode.net/opensource/sharpziplib/ 下载SharpZiplib的最新版本,支持Zip, GZip, BZip2 和Tar格式 ...
- 【转】iOS开发:开发证书知识点总结
原文网址:http://www.jianshu.com/p/9c166a5e4930 1. Error: An App ID with identifier "*" is not ...
- 【转】三十三、Android给ListView设置分割线Divider样式
原文网址:http://www.cnblogs.com/linjiqin/archive/2011/11/12/2246349.html 给ListView设置分割线,只需设置如下两个属性: andr ...
- extjs form 取值 赋值 重置
一.从form中获取field的三个方法: 1.Ext.getCmp('id'); 2.FormPanel.getForm().findField('id/name'); 3.Ext.get('id/ ...
- LoadRunner_Analysis(z) 分析
LoadRunner_Analysis(z) 分析 lr_Analysis(z) Analysis Summary Page Analysis Summary(分析总结页面) 分为三个部分: Stat ...
- 算法 python实现(三) 快速排序
算法学起来真费劲啊,智商只够捉只鸡的.昨晚没看明白就没电了,过两天要考虑偷电了... 今天看看快速排序,有一个博客写的很好,通俗生动形象,适合我这样的算法大白菜.推荐一下 http://www.cnb ...