类模板
What's the Class Template?
定义:可以被一种或多种类型参数化的类
How Template?
-
template <class T>
-
class Stack
-
{
-
...
-
};
在类模板内部,T可以像其他任何类型一样,用于声明成员变量和成员函数:
-
template <typename T>
-
class Stack {
-
private:
-
std::vector<T> elems; // elements
-
-
public:
-
Stack(); // constructor
-
void push(T const&); // push element
-
void pop(); // pop element
-
T top() const; // return top element
-
};
类的类型是Stack,需要使用该类的类型时,你必须使用Stack:
-
template <typename T>
-
class Stack {
-
…
-
Stack (Stack<T> const&); // copy constructor
-
Stack<T>& operator= (Stack<T> const&); // assignment operator
-
…
-
};
当使用类名而不是用类的类型时,就应该用Stack.
成员函数
必须指定成员函数是个模板函数:
-
template <typename T>
-
void Stack<T>::push (T const& elem)
-
{
-
elems.push_back(elem); // append copy of passed elem
-
}
类模板调用
只用被调用的成员函数才会被实例化,这样来节省空间和时间;另外一个原因是,用一些特殊且仅支持部分模板类成员的参数来实例化类模板:
-
// basics/stack1test.cpp
-
-
#include <iostream>
-
#include <string>
-
#include <cstdlib>
-
#include "stack1.hpp"
-
-
int main()
-
{
-
try {
-
Stack<int> intStack; // stack of ints
-
Stack<std::string> stringStack; // stack of strings
-
-
// manipulate int stack
-
intStack.push(7);
-
std::cout << intStack.top() << std::endl;
-
-
// manipulate string stack
-
stringStack.push("hello");
-
std::cout << stringStack.top() << std::endl;
-
stringStack.pop();
-
stringStack.pop();
-
}
-
catch (std::exception const& ex) {
-
std::cerr << "Exception: " << ex.what() << std::endl;
-
return EXIT_FAILURE; // exit program with ERROR status
-
}
-
}
类模板特化
可以用模板实参来特化模板类,和函数模板的重载类似,通过特化类模板,可以优化基于某种特定类型的实现或者克服某种特定类型在实例化类模板时所出现的问题。
特定类:
-
template<>
-
class Stack<std::string> {
-
…
-
};
特定成员:
-
void Stack<std::string>::push (std::string const& elem)
-
{
-
elems.push_back(elem); // append copy of passed elem
-
}
局部特化
-
// partial specialization: both template parameters have same type
-
template <typename T>
-
class MyClass<T,T> {
-
…
-
};
-
-
// partial specialization: second type is int
-
template <typename T>
-
class MyClass<T,int> {
-
…
-
};
-
-
// partial specialization: both template parameters are pointer types
-
template <typename T1, typename T2>
-
class MyClass<T1*,T2*> {
-
…
-
};
缺省模板参数
-
// basics/stack3.hpp
-
-
#include <vector>
-
#include <stdexcept>
-
-
template <typename T, typename CONT = std::vector<T> >
-
class Stack {
-
private:
-
CONT elems; // elements
-
-
public:
-
void push(T const&); // push element
-
void pop(); // pop element
-
T top() const; // return top element
-
bool empty() const { // return whether the stack is empty
-
return elems.empty();
-
}
-
};
-
-
template <typename T, typename CONT>
-
void Stack<T,CONT>::push (T const& elem)
-
{
-
elems.push_back(elem); // append copy of passed elem
-
}
-
-
template <typename T, typename CONT>
-
void Stack<T,CONT>::pop ()
-
{
-
if (elems.empty()) {
-
throw std::out_of_range("Stack<>::pop(): empty stack");
-
}
-
elems.pop_back(); // remove last element
-
}
-
-
template <typename T, typename CONT>
-
T Stack<T,CONT>::top () const
-
{
-
if (elems.empty()) {
-
throw std::out_of_range("Stack<>::top(): empty stack");
-
}
-
return elems.back(); // return copy of last element
-
}
阅读(1221) | 评论(0) | 转发(0) |