c++沉思录笔记(14章代码)

第十四章比较简单,只是在十三章的基础上增添了一些关系函数、自增函数等等。

看看学完这章我们的主函数能做些什么

#include "stdafx.h"
#include "Array.h"
#include "Pointer.h"
#include "Ptr_to_const.h"
#include<iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	Array<int>* ap = new Array<int> (10);
	Pointer<int> beg(*ap, 0);
	Pointer<int> end(*ap, 9);
	cout << end - beg << endl;
	while (beg != end)
		*beg++ = 42;
	Pointer<int> beg1(*ap, 0);
	while (beg1 != end)
		cout << *beg1++ << "  ";
	cout << endl;
	Ptr_to_const<int> p(*ap,2);
	//cout << *p << endl;
	//*p = 21; Ptr_to_const是指向常量的指针
	cout << *p << endl;
	return 0;
}

接下来就是完整的代码

#ifndef ARRAY_H
#define ARRAY_H
#include "stdafx.h"
#include "Array_data.h"
template <typename T> class Array_data;
template <typename T> class Pointer;
template<typename T>
class Array {
	friend class Ptr_to_const<T>;
	friend class Pointer<T>;
public:
	Array();
	Array(unsigned size);
	Array(const Array<T>&);
	~Array();
	const T& operator[](unsigned n) const;
	T& operator[](unsigned n);
	void resize(unsigned n);
	void reserve(unsigned new_sz);
	Array& operator=(const Array<T>&);
	bool IsNull();
	int useCount();
private:
	Array_data<T>* data;
};
#include"Array.cpp"
#endif

#include "stdafx.h"
#include "Array.h"
#include "Array_data.h"

template <typename T>
Array<T>::Array():data(0) { }

template <typename T>
Array<T>::Array(unsigned size):data(new Array_data<T>(size)) { }

template <typename T>
Array<T>::Array(const Array<T>& a):data(new Array_data<T>(a.data->sz))
{
	data->copy(a.data->data , a.data->sz);
}


template <typename T>
Array<T>::~Array()
{
	if(--data->use == 0)
		delete data;
}

template <typename T>
const T& Array<T>::operator[](unsigned n) const
{
	return (*data)[n];
}

template <typename T>
T& Array<T>::operator[](unsigned n)
{
	return (*data)[n];
}

template <typename T>
void Array<T>::resize(unsigned n)
{
	data->resize(n)
}

template <typename T>
void Array<T>::reserve(unsigned new_sz)
{
	if (new_sz >= data->sz)
		data->grow(new_sz);
}

template <typename T>
Array<T>& Array<T>::operator=(const Array<T>& a)
{
	if (this != &a)
			data->clone(*a.data , a.data->data);
	return *this;
}
///
//14章内容
template <typename T>
bool Array<T>::IsNull()
{
	return data->IsNull();
}
template <typename T>
int Array<T>::useCount()
{
	return data->useCount();
}


#ifndef ARRAY_DATA_H_INCLUDED
#define ARRAY_DATA_H_INCLUDED
#include "stdafx.h"
template <typename T> class Array;
template <typename T> class Ptr_to_const;
template <typename T> class Pointer;
template <typename T>
class Array_data {
	friend class Array<T>;
	friend class Ptr_to_const<T>;
	friend class Pointer<T>;
	Array_data(unsigned s = 0);
	~Array_data();
	T& operator[](unsigned i);
	const T& operator[](unsigned i) const;
	Array_data(const Array_data&);
	Array_data& operator=(const Array_data&);
	bool IsNull();
	int useCount();
	void resize(unsigned n);
	void copy(T* arr, unsigned n);
	void grow(unsigned new_sz);
	void clone(const Array_data<T>& a, unsigned n);
	T* data;
	unsigned sz;
	int use;				//引用计数
};
#include "Array_data.cpp"
#endif // ARRAY_DATA_H_INCLUDED

#include "stdafx.h"
#include "Array_data.h"

template <typename T>
Array_data<T>::Array_data(unsigned s = 0):
						data(new T[s]), sz(s), use(1) { }

template <typename T>
Array_data<T>::~Array_data() { delete []data; }

template <typename T>
T& Array_data<T>::operator[](unsigned i)
{
	if(i >= sz)
		throw "Array_data subscript out of range";
	return data[i];
}

template <typename T>
const T& Array_data<T>::operator[](unsigned i) const
{
	if(i >= sz)
		throw "Array_data subscript out of range";
	return data[i];
}

template <typename T>
void Array_data<T>::resize(unsigned n)
{
	if (n == sz)	return;
	T* odata = data;
	data = new T[n];
	copy(odata, sz > n? n : sz);
	delete [] odata;
	sz = n;
}

template <typename T>
void Array_data<T>::copy(T* arr, unsigned n)
{
	for(unsigned i = 0; i != n ; ++i)
		data[i] = arr[i];
}

template <typename T>
void Array_data<T>::grow(unsigned new_sz)
{
	unsigned nsz = sz;
	if (nsz == 0)
		nsz = 1;
	while(nsz <= new_sz)
		nsz *= 2;
	return nsz;
}

template <typename T>
void Array_data<T>::clone(const Array_data<T>& a , unsigned n)
{
	delete [] data;
	data = new T [sz = a.sz];
	copy(a.data, sz);
}
/
//14章内容
template <typename T>
bool Array_data<T>::IsNull()
{
	return (use == 0);
}
template <typename T>
int Array_data<T>::useCount()
{
	return use;
}

#ifndef PTR_TO_CONST_H_INCLUDED
#define PTR_TO_CONST_H_INCLUDED

template <typename T> class Array;	
template <typename T> class Array_data;
template <typename T> 
bool operator==(const Ptr_to_const<T>&, const Ptr_to_const<T>&);
template <typename T> 
bool operator!=(const Ptr_to_const<T>&, const Ptr_to_const<T>&);
template <typename T>
int operator-(const Ptr_to_const<T>&, const Ptr_to_const<T>&);

template <typename T>
class Ptr_to_const {
public:
	friend bool operator== <T> (const Ptr_to_const<T>&, const Ptr_to_const<T>&);
	friend bool operator!= <T> (const Ptr_to_const<T>&, const Ptr_to_const<T>&);
	friend int operator- <T> (const Ptr_to_const<T>&, const Ptr_to_const<T>&);

	// const Array& 而不是Array&
	Ptr_to_const(const Array<T>& a, unsigned n = 0);
	Ptr_to_const();
	Ptr_to_const(const Ptr_to_const<T>& p);
	~Ptr_to_const();
	Ptr_to_const& operator=(const Ptr_to_const<T>&);

	//返回 const T&, 而不是T&
	const T& operator*() const;

	//定义加法、减法、关系操作符
	Ptr_to_const& operator++();
	Ptr_to_const& operator--();
	Ptr_to_const operator++(int);
	Ptr_to_const operator--(int);
	Ptr_to_const& operator+=(int n);
	Ptr_to_const& operator-=(int n);
protected:
	Array_data<T>* ap;
	unsigned sub;
};
#include "Ptr_to_const.cpp"
#endif

#include "stdafx.h"
#include "Ptr_to_const.h"
template <typename T>
Ptr_to_const<T>::Ptr_to_const(const Array<T>& a, unsigned n = 0):
			ap(a.data), sub(n) { ++ap->use; }

template <typename T>
Ptr_to_const<T>::Ptr_to_const(): ap(0), sub(0) { }

template <typename T>
Ptr_to_const<T>::Ptr_to_const(const Ptr_to_const<T>& p):
			ap(p.ap), sub(p.sub) { ++ap->use; }

template <typename T>
Ptr_to_const<T>::~Ptr_to_const()
{
	if (--ap->use == 0)
		delete ap;
}

template <typename T>
Ptr_to_const<T>& Ptr_to_const<T>::operator=(const Ptr_to_const<T>& p)
{
	if (p.ap)
		++p.ap->use;
	if (ap && --ap->use == 0)
		delete ap;
	ap = p.ap;
	sub = p.sub;
	return *this;
}

//返回 const T&, 而不是T&
template <typename T>
const T& Ptr_to_const<T>::operator*() const
{
	if (ap == 0)
		throw "* of unbound Ptr_to_const";
	return (*ap) [sub];
}

/
//第14章内容

template <typename T>
Ptr_to_const<T>& Ptr_to_const<T>::operator++()
{
	++sub;
	return *this;
}
template <typename T>
Ptr_to_const<T>& Ptr_to_const<T>::operator--()
{
	--sub;
	return *this;
}
template <typename T>
Ptr_to_const<T> Ptr_to_const<T>::operator++(int)
{
	Ptr_to_const<T> p = *this;
	++sub;
	return p;
}
template <typename T>
Ptr_to_const<T> Ptr_to_const<T>::operator--(int)
{
	Ptr_to_const<T> p = *this;
	--sub;
	return p;
}
template <typename T>
Ptr_to_const<T>& Ptr_to_const<T>::operator+=(int n)
{
	sub += n;
	return *this;
}
template <typename T>
Ptr_to_const<T>& Ptr_to_const<T>::operator-=(int n)
{
	sub -= n;
	return *this;
}
//友元函数
template <typename T>
bool operator==(const Ptr_to_const<T>& p1, const Ptr_to_const<T>& p2)
{
	if(p1.ap != p2.ap)	return false;
	return (p1.sub == p2.sub);
}
template <typename T>
bool operator!=(const Ptr_to_const<T>& p1, const Ptr_to_const<T>& p2)
{
	if(p1.ap != p2.ap)	return false;
	return (p1.sub != p2.sub);
}
template <typename T>
int operator-(const Ptr_to_const<T>& p1, const Ptr_to_const<T>& p2)
{
	return (int)p1.sub - (int)p2.sub; 
}

#ifndef POINTER_H_INCLUDED	
#define POINTER_H_INCLUDED
#include "stdafx.h"
#include "Ptr_to_const.h"
template <typename T> class Array;
template <typename T> class Array_data;
template <typename T> 
bool operator==(const Pointer<T>&, const Pointer<T>&);
template <typename T> 
bool operator!=(const Pointer<T>&, const Pointer<T>&);

template <typename T>
class Pointer : public Ptr_to_const<T>{
	friend bool operator== <T> (const Pointer<T>&, const Pointer<T>&);
	friend bool operator!= <T> (const Pointer<T>&, const Pointer<T>&);
public:
	Pointer(Array<T>& a, unsigned n = 0);
	Pointer();
	Pointer(const Pointer&);
	T& operator*() const;

	//定义加法、减法、关系操作符
	Pointer& operator++();
	Pointer& operator--();
	Pointer operator++(int);
	Pointer operator--(int);
	Pointer& operator+=(int n);
	Pointer& operator-=(int n);
};

#include "Pointer.cpp"
#endif // POINTER_H_INCLUDED

template <typename T>
Pointer<T>::Pointer(Array<T>& a, unsigned n):
					Ptr_to_const<T>(a, n) { }

template <typename T>
Pointer<T>::Pointer():Ptr_to_const<T>() { }

template <typename T>
T& Pointer<T>::operator*() const
{
	if (ap == 0)
		throw "* of unbound Pointer";
	return (*ap) [sub];
}

template <typename T>
Pointer<T>::Pointer(const Pointer& p):Ptr_to_const(p) {  }

///
//14章内容
template <typename T>
Pointer<T>& Pointer<T>::operator++()
{
	++sub;
	return *this;
}
template <typename T>
Pointer<T>& Pointer<T>::operator--()
{
	--sub;
	return *this;
}
template <typename T>
Pointer<T> Pointer<T>::operator++(int)
{
	Pointer<T> p = *this;
	++sub;
	return p;
}
template <typename T>
Pointer<T> Pointer<T>::operator--(int)
{
	Pointer<T> p = *this;
	--sub;
	return p;
}
//友元函数
template <typename T>
bool operator==(const Pointer<T>& p1, const Pointer<T>& p2)
{
	if(p1.ap != p2.ap)	return false;
	return (p1.sub == p2.sub);
}
template <typename T>
bool operator!=(const Pointer<T>& p1, const Pointer<T>& p2)
{
	if(p1.ap != p2.ap)	return false;
	return (p1.sub != p2.sub);
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值