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:
Note that we now have two template parameters, so each
definition of a member function must be defined with these two parameters:
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:
In addition, you could specify the container for the elements
when you declare a Stack object in your program:
With
you declare a stack for doubles that uses a
std::deque<> to manage the elements internally.
-----------------------------------------------------------------
See Also:
-----------------------------------------------------------------
See Also:
-----------------------------------------------------------------
- Complete Tutorial of C++ Template's
- Standard Template Library Tutorial
- Inter Process Communication Tutorial
- Advance Programming in C & C++
-----------------------------------------------------------------
No comments:
Post a Comment