第16章 模板與泛型程式設計 13

2021-06-02 16:58:37 字數 789 閱讀 1958

16.5 乙個泛型控制代碼類

16.5.1 定義控制代碼類

#ifndef handle_h

#define handle_h

#include "stdafx.h"

#include using namespace std;

templateclass handle

t& operator*();

t* operator->();

const t& operator*() const;

const t* operator->() const;

handle(const handle& h):ptr(h.ptr), use(h.use)

handle& operator=(const handle&);

~handle()

private:

t* ptr;

size_t *use;

void rem_ref()

}};templateinline handle& handle::operator=(const handle &rhs)

template inline t& handle::operator*()

template inline t* handle::operator->()

#endif

16.5.2 使用控制代碼

handlehandle(new int(42));

{ handlehp2=handle;

cout<<*handle<<" "<<*hp2<

第16章 模板與泛型程式設計 10

16.4.4 queue和queueitem的友元宣告 1.將類模板設為友元 template class queueitem type item queueitem next friend class queue 2.queue輸出操作符 template ostream operator ost...

第16章 模板與泛型程式設計 15

16.6.2 類模板的特化 1.定義類特化 template class queue const string front const void push const char void pop bool empty const friend ostream operator ostream os,...

第16章 模板與泛型程式設計 3

16.1.3 模板型別形參 型別形參由關鍵字class或typename後接說明符構成。在模板形參表中,這兩個關鍵字具有相同的含義,都指出後面所接的名字表示乙個型別。模板型別形參可作為型別說明符用在模板中的任何地方,與內建型別說明符或類型別說明符的使用方式完全相同。具體而言,它可以用於指定返回型別或...