Ru-Brd |
For class templates you can also define default values for template parameters. These values are called default template arguments . They may even refer to previous template parameters. For example, in class Stack<> you can define the container that is used to manage the elements as a second template parameter, using std::vector<> as the default value: // 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 } Note that we now have two template parameters, so each definition of a member function must be defined with these two parameters: template <typename T, typename CONT> void Stack<T,CONT>::push (T const& elem) { elems.push_back(elem); // append copy of passed elem } You can use this stack the same way it was used before. Thus, if you pass a first and only argument as an element type, a vector is used to manage the elements of this type: template <typename T, typename CONT = std::vector<T> > class Stack { private: CONT elems; // elements }; In addition, you could specify the container for the elements when you declare a Stack object in your program: // basics/stack3test.cpp #include <iostream> #include <deque> #include <cstdlib> #include "stack3.hpp" int main() { try { // stack of int s: Stack<int> intStack; // stack of double s which uses a std::deque<> to mange the elements Stack<double,std::deque<double> > dblStack; // manipulate int stack intStack.push(7); std::cout << intStack.top() << std::endl; intStack.pop(); // manipulate double stack dblStack.push(42.42); std::cout << dblStack.top() << std::endl; dblStack.pop(); dblStack.pop(); } catch (std::exception const& ex) { std::cerr << "Exception: " << ex.what() << std::endl; return EXIT_FAILURE; // exit program with ERROR status } } With Stack<double,std::deque<double> > you declare a stack for double s that uses a std::deque<> to manage the elements internally. |
Ru-Brd |