TAILIEUCHUNG - thinking in c 2nd ed volume 2 rev 20 - phần 4

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 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 t T begin return T end return 158z516 int main Container int Array theData l 2 int p while p 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

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.