Đang chuẩn bị liên kết để tải về tài liệu:
thinking in c 2nd ed volume 2 rev 20 - phần 4

Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ

Minh họa một mẫu tham số mẫu # include # bao gồm bằng cách sử dụng tên miền không gian tiêu chuẩn; mẫu lớp Array {/ / chuỗi đơn giản, mở rộng enum {init = 10} T * dữ liệu; size_t năng lực; size_t count; công: Array () {count = 0; dữ liệu = new T [công suất = init];} trống push_back (const T & t) {if (count == công suất) | 157z516 strange since templates are types and type parameters are already allowed but if you are going to use a template type parameter as a template in your code the compiler needs to know that the parameter is a template in the first place. The following example illustrates a template template parameter. Comment C05 TempTemp.cpp Illustrates a template template parameter include cstddef include iostream using namespace std template class T class Array A simple expandable sequence enum iNiT 10 T data size_t capacity size_t count public Array count 0 data new T capacity INIT void push_back const T t if count capacity Grow underlying array size_t newCap 2 capacity T newData new T newCap for size_t i 0 i count i newData i data i delete data data newData capacity newCap data count t void pop_back if count 0 --count T begin return data T end return data count template class T template class class Seq class Container Seq T seq public void append const T t seq.push_back t T begin return seq.begin T end return seq.end 158z516 int main Container int Array theData theData.append l theData.append 2 int p theData.begin while p theData.end cout p endl The Array class template is a trivial sequence container. The Container template takes two parameters the type of the objects it is to hold and a sequence data structure to do the holding. The following line in the implementation of the Container class requires that we inform the compiler that Seq is a template C mment Seq T seq If we hadn t declared Seq to be a template template parameter the compiler would complain here that Seq is not a template since we re using it as such. In main a Container is instantiated to use an Array to hold integers so Seq stands for Array in this example. Comment Note that it is not necessary in this case to name the parameter for Seq inside Container s declaration. The line in question is template class T template class class Seq Although we could have written template class T template class U class