Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

ANSI/ISO C++ Professional Programmer''''s Handbook phần 7 pps
Nội dung xem thử
Mô tả chi tiết
const T& operator [] (unsigned int index) const;
size_t size() const;
};
Member functions of a class template can be defined outside the class body. In this case, they have to be explicitly
declared as member functions of their class template. For example
//definitions of Vector's member functions
//follow the class declaration
template <class T> Vector<T>::Vector<T> (size_t s) //constructor definition
: sz(s),
buff (new T[s])
{}
template <class T> Vector<T>::Vector<T> (const Vector <T> & v) //copy ctor
{
sz = 0;
buff = 0;
*this = v; //use overloaded assignment operator
}
template <class T> Vector<T>& Vector<T>::operator= // assignment operator
(const Vector <T> & v)
{
if (this == &v)
return *this;
this->Vector<T>::~Vector<T>(); //call destructor
buff = new T[v.size()]; //allocate sufficient storage
for (size_t i =0; i < v.size(); i++)
buff[i] = v[i]; //memberwise copy
sz = v.size();
return *this;
}
template <class T> Vector<T>::~Vector<T> () //destructor
{
delete [] buff;
}
template <class T> inline T& Vector<T>::operator [] (unsigned int i)
{
return buff[i];
}
template <class T> inline const T& Vector<T>::operator [] //const version
(unsigned int i) const
{
return buff[i];
}
template <class T> inline size_t Vector<T>::size () const
{
return sz;
}
//Vector.hpp
The prefix template <class T> indicates that T is a template parameter, which is a placeholder for a yet
unspecified type. The keyword class is of particular interest because the corresponding argument for the parameter
ANSI/ISO C++ Professional Programmer's Handbook - Chapter 9 - Templates
file:///D|/Cool Stuff/old/ftp/1/1/ch09/ch09.htm (3 von 22) [12.05.2000 14:46:22]
T is not necessarily a user-defined type; it can also be a fundamental type, such as char or int. If you prefer a more
neutral term, you can use the typename keyword instead (typename also has other uses, though, as you will see
next):
template <typename T> class Vector //typename instead of class
//no semantic difference between the two forms
{
//...
};
template <typename T> Vector<T>::Vector<T> (size_t s)
: sz(s), buff (new T[s]) {}
Within the scope of Vector, qualification with the parameter T is redundant, so the member functions can be
declared and defined without it. The constructor, for example, can be declared as follows:
template <class T> class Vector
{
public:
Vector (size_t s = 100); // equivalent to Vector <T>(size_t s = 100);
};
Similarly, the constructor's definition can omit the redundant parameter:
// equivalent to template <class T> Vector<T>::Vector<T>(size_t s)
template <class T> Vector<T>::Vector (size_t s) :
buff (new T[s]), sz(s)
{}
Instantiation and Specialization
A class template is not a class. The process of instantiating a class from a class template and a type argument is called
template instantiation. A template id, that is, a template name followed by a list of arguments in angular brackets (for
example, Vector<int>), is called a specialization. A specialization of a class template can be used exactly like any
other class. Consider the following examples:
void func (Vector <float> &); //function parameter
size_t n = sizeof( Vector <char>); //sizeof expression
class myStringVector: private Vector<std::string> //base class
{/*...*/};
#include <iostream>
#include <typeinfo>
#include <string>
using namespace std;
cout<<typeid(Vector< string>).name(); //typeid expression
Vector<int> vi; // creating an object
The compiler instantiates only the necessary member functions of a given specialization. In the following example,
points of member instantiation are numbered:
#include <iostream>
#include "Vector.hpp"
ANSI/ISO C++ Professional Programmer's Handbook - Chapter 9 - Templates
file:///D|/Cool Stuff/old/ftp/1/1/ch09/ch09.htm (4 von 22) [12.05.2000 14:46:22]