《C++ Primer Plus》14.4 类模板 学习笔记

14.4.1 定义类模板
下面以第10章的Stack类为基础来建立模板。原来的类声明如下:
typedef unsigned long Item;

class Stack
{
private:
    enum {MAX = 10};    // constant specific to class
    Item items[MAX];    // holds stack items
    int top;            // index for top stack item
public:
    Stack();
    bool isempty() const;
    bool isfull() const;
    // push() returns false if stack already is full, true otherwise
    bool push(const Item & item);   // add item to stack
    // pop() returns false if stack already is empty, true otherwise
    bool pop(Item & item);          // pop top into item
};
采用模板时,将使用模板定义替换Stack声明,使用模板成员函数替换Stack的成员函数。和模板函数一样,模板类以下面这样的代码开头:
template <class Type>
关键字template告诉编译器,将要定义一个模板。尖括号中的内容相当于函数的参数列表。可以把关键字class看作是变量的类型名,该变量接受类型作为其值,把Type看作是改变量的名称。
这里使用class并不意味着Type必须是一个类;而只是表明Type是一个通用的类型说明符,在使用模板时,将使用实际的类型替换它。较新的C++实现允许在这种情况下使用不太容易混淆的关键字typename代替class:
template <typename Type>    // new choice
可以使用自己的泛型名代替Type,其命名规则与其他标识符相同。当前比较流行的选项包括T和Type。在模板定义中,可以使用泛型名来标识要存储在栈中的类型。对于Stack来说,这意味着将声明中所有的typedef标识符Item替换为Type。例如,
Item items[MAX];    // holds stack items
应改为:
Type items[MAX];    // holds stack items
同样,可以使用模板成员函数替换原有类的类方法。每个函数头都将以相同的模板声明打头:
template <class Type>
同样应使用泛型名Type替换typedef标识符Item。另外,还需要将限定符从Stack::改为Stack<Type>::。例如,
bool Stack::push(const Item & item)
{
...
}
应改为:
template <class Type>       // or template <typename Type>
bool Stack<Type>::push(const Type & item)
{
...
}
如果在类声明中定义了方法(内联定义),则可以省略模板前缀和类限定符。
程序清单14.13列出了类模板和成员函数。知道这些模板不是类和成员函数定义至关重要。它们是C++编译器指令,说明了如何生成类和成员函数定义。模板的具体是现——如用来处理string对象的栈类——被称为实例化(instantiation)或具体化(specialization)。不能将模板成员函数放在独立的实现文件中。由于模板不是函数,它们不能单独编译。模板必须与特定的模板实例化请求一起使用。为此,最简单的方法是将所有模板信息放在一个头文件中,并在要使用这些模板的文件中包含该文件头。
程序清单14.13 stacktp.h

// stacktp.h -- a stack template
#ifndef STACKTP_H_
#define STACKTP_H_

template <class Type>
class Stack
{
private:
    enum {MAX = 10};    // constant specific to class
    Type items[MAX];    // holds stack items
    int top;            // index for top stack item
public:
    Stack();
    bool isempty();
    bool isfull();
    bool push(const Type & item);   // add item to stack
    bool pop(Type & item);          // pop top into item
};

template <class Type>
Stack<Type>::Stack()
{
    top = 0;
}

template <class Type>
bool Stack<Type>::isempty()
{
    return top == 0;
}

template <class Type>
bool Stack<Type>::isfull()
{
    return top == MAX;
}

template <class Type>
bool Stack<Type>::push(const Type & item)
{
    if (top < MAX)
    {
        items[top++] = item;
        return true;
    }
    else
        return false;
}

template <class Type>
bool Stack<Type>::pop(Type & item)
{
    if (top > 0)
    {
        item = items[--top];
        return true;
    }
    else
        return false;
}

#endif // STACKTP_H_

14.4.2 使用模板类
仅在程序包含模板并不能生成模板类,而必须请求实例化。为此,需要生命一个类型为模板类的对象,方法是使用所需的具体类型替换泛型名。例如,下面的代码创建两个栈,一个用于存储int,另一个用于存储string对象:
Stack<int> kernels;        // create a stack of ints
Stack<string> colonels;        // create a stack of string objects
看到上述声明后,编译器将按Stack<Type>模板来生成两个独立的类声明和两组独立的类方法。
程序清单14.14修改了原来的栈测试程序(程序清单11.12),使用字符串而不是unsigned long值作为订单ID。
程序清单14.14 stacktem.cpp

// stacktem.cpp -- testing the template stack class
#include <iostream>
#include <string>
#include <cctype>
#include "stacktp.h"
using std::cin;
using std::cout;

int main()
{
    Stack<std::string> st;  // create an empty stack
    char ch;
    std::string po;
    cout << "Please enter A to add a purchase order.\n"
         << "P to process a PO, or Q to quit.\n";
    while (cin >> ch && std::toupper(ch) != ‘Q‘)
    {
        while (cin.get() != ‘\n‘)
            continue;
        if (!std::isalpha(ch))
        {
            cout << ‘\a‘;
            continue;
        }
        switch (ch)
        {
            case ‘A‘:
            case ‘a‘:   cout << "Enter a PO number to add: ";
                        cin >> po;
                        if (st.isfull())
                            cout << "stack already full\n";
                        else
                            st.push(po);
                        break;
            case ‘P‘:
            case ‘p‘:   if (st.isempty())
                            cout << "stack already empty\n";
                        else {
                            st.pop(po);
                            cout << "PO #" << po << " popped\n";
                            break;
                        }
        }
        cout << "Please enter A to add a purchase order.\n"
             << "P to process a PO, or Q to quit.\n";
    }
    cout << "Bye\n";
    return 0;
}

效果:

Please enter A to add a purchase order.
P to process a PO, or Q to quit.
A
Enter a PO number to add: moonlightpoet
Please enter A to add a purchase order.
P to process a PO, or Q to quit.
A
Enter a PO number to add: moonlit
Please enter A to add a purchase order.
P to process a PO, or Q to quit.
P
PO #moonlit popped
Please enter A to add a purchase order.
P to process a PO, or Q to quit.
P
PO #moonlightpoet popped
Please enter A to add a purchase order.
P to process a PO, or Q to quit.
P
stack already empty
Please enter A to add a purchase order.
P to process a PO, or Q to quit.
Q
Bye
时间: 2024-08-02 07:31:06

《C++ Primer Plus》14.4 类模板 学习笔记的相关文章

初步C++类模板学习笔记

类模板 实现:在上课时间的定义给它的一个或多个参数,这些参数代表了不同的数据类型.                              -->抽象的类. 在调用类模板时, 指定參数, 由编译系统依据參数提供的数据类型自己主动产生对应的模板类                   -->详细的类. 类模板的定义 C++的类模板的写法例如以下: template <类型參数表> //类型參数表的写法就是:class 类型參数1, class 类型參数2, - class 类模板名

初探C++类模版学习笔记

类模板 实现:在定义类的时候给它一个或多个参数,这个些参数表示不同的数据类型.                              -->抽象的类. 在调用类模板时, 指定参数, 由编译系统根据参数提供的数据类型自动产生相应的模板类                   -->具体的类. 类模板的定义 C++的类模板的写法如下: template <类型参数表> //类型参数表的写法就是:class 类型参数1, class 类型参数2, - class 类模板名 { 成员函数

控件模板学习笔记(二)

1.模板绑定TemplateBinding 什么情况下使用模板绑定? --当您仍可能想要使用公共属性更改控件的外观时,模板绑定是您的不二之选 怎么使用模板绑定? --我们还以学习笔记一中的Button为例,自定义模板中的Border的Background=“Red”,使用TemplateBinding则为Background=“{TemplateBinding Backbround}”: 等号左边的Background为Border的背景颜色,等号右边的Background为Button的属性.

《C++ Primer Plus》14.2 私有继承 学习笔记

C++(除了成员变量之外)还有另一种实现has-a关系的途径——私有继承.使用私有继承,基类的公有成员和保护成员都将成为派生类的私有成员.(如果使用保护继承,基类的公有成员和保护成员都将称为派生类的保护成员.)这意味着基类方法将不会称为派生类对象共有接口的一部分,但可以在派生类的成员函数中使用它们.14.2.1 Student类示例(新版本)Student类应从两个类派生而来,因此声明将列出这两个类:class Student : private std::string, private std

c++ primer(第五版)学习笔记及习题答案代码版(第十四章)重载运算与类型转换

笔记较为零散,都是自己不熟悉的知识点. 习题答案至于一个.h 和.cc 中,需要演示某一题直接修改 #define NUM****, 如运行14.30题为#define NUM1430: Alice Emma has long flowing red hair. Her Daddy says when the wind blows through her hair, it looks almost alive, like a fiery bird in flight. A beautiful f

c++ primer(第五版)学习笔记及习题答案代码版(第六章)函数

笔记较为零散,都是自己不熟悉的知识点. 习题答案至于一个.cc 中,编译需要包含Chapter6.h头文件. 需要演示某一题直接修改 #define NUM***, 如运行6.23题为#define NUM623: chapter 6 1. 形参初始化的机理与变量初始化一样. 当形参是引用类型时,它对应的实参被引用传递或者函数被传引用调用. 2. const和实参 void fcn(const int i){ /*fcn能够读取i,但是不能向i写值*/} void fcn(int i){ /*.

tornada模板学习笔记

import tornado.web import tornado.httpserver import tornado.ioloop import tornado.options import os.path from tornado.options import define, options define("port", default=8000, help="run on the given port", type=int) class HelloHandle

《C++ Primer Plus》16.4 泛型编程 学习笔记

STL是一种泛型编程(generic programming).面向对象编程关注的是编成的数据方面,而泛型编程关注的是算法.它们之间的共同点是抽象和创建可重用代码,单他们的理念决然不同.泛型编程旨在编写独立于数据类型的代码. 16.4.1 为何使用迭代器理解迭代器是理解STL的关键所在.模板使得算法独立于存储的数据类型,而迭代其使算法独立于使用的容器类型.因此,它们都是STL通用方法的重要组成部分.为了解为何需要迭代器,我们来看如何为两种不同数据表现实现find函数,然后来看如何推广这种方法.首

《C++ Primer Plus》第4章 学习笔记

数组.结构和指针是C++的3中符合类型.数组可以在一个数据对象中存储多个同种类型的值.通过使用索引或下标,可以访问数组中各个元素.结构可以将多个不同类型的值存储在同一个数据对象中,可以使用成员关系运算符(.)来访问其中的成员.使用结构的第一步是创建结构模板,它定义结构存储了那些成员.模板的名称将称为新类型的标识符,然后就可以声明这种类型的结构变量.共用体可以存储一个值,但是这个值可以是不同的类型,成员名指出了使用的模式.指针是被设计用来存储地址的变量.我们说,指针指向它存储的地址.指针声明指出了