C + + Learning Note 36: Class templates

Source: Internet
Author: User

Purpose of the class template

    • Design generic types to accommodate a wide range of member data patterns

Definition Format for class templates

    • template< template Form parameter list >class class name {...};
    • Prototype: Template<typename t> class A;

Members of class templates

    • Define as a member of a normal class
    • Definitions are available in a class or outside a class, which needs to list template parameters after the class name to distinguish member functions from non-template classes
    • Template<typename t> T a<t>::f (u)

Template for class member functions

    • member functions can use other templates
Template<typename t>class a{public:    template<typename u> T f (const U &u);}; Template<typename t> template<typename u>t a<t>::f (const U &u) {}

Class template

The mass of a class template

    • Unlike function templates, when a class template is instantiated, the actual parameters of the template must be given, such as:a<t> A;
    • When a class template is instantiated, the compiler generates code for the template class or member function, the member function is instantiated at invocation, and the virtual function is structured in class

Explicit formatting of class templates

    • Template class a<int>;
    • To solve the template library creation problem, the user of the library may not have the opportunity to be in the body, and the non-structured template definition will not appear in the target file
    • After explicitly formatting a class template, explicitly body its constructor
    • Other member functions can be either explicit or non-explicit

Explicit specificity of class templates

    • Use a specific type or value to explicitly type the class template to customize the class template code, such as:template<> class a<char>{...};
    • The explicit version of the release covers the body version
    • Explicit specificity does not require the same as the original template, the special version can have different data members or member functions
    • Class templates can be partially specific, and the results are still class templates to support partial customization of class templates

Default template parameters for class templates

    • As with function templates, class templates can have default template parameters
//Queue#include <iostream>#include<cstdlib>//Empty queue Exception classclassEqueueempty {};//queue Item class predecessor declarationTemplate<typename t>classJuqueueitem;//Queue classTemplate<typename t>classjuqueue{ Public: Juqueue:_head (null), _tail (null) {}VirtualJuqueue (); Virtual voidEnter (ConstT &item); VirtualT Leave (); BOOLIsEmpty ()Const{return_head = =0; }Private: Juqueueitem<T> *_head, *_tail;};//queue item class, single chain table structureTemplate<typename t>classjuqueueitem{friendclassJuqueue<t>; Public: Juqueueitem (ConstT &item): _item (item), _next (0){}Private: T _item; Juqueueitem<T> *_next;};//Queue class destructorTemplate<typename t> juqueue<t>::~Juqueue () { while(!IsEmpty ())    {Leave (); }}//QueueTemplate<typename t>voidJuqueue<t>::enter (ConstT &Item) {Juqueueitem<T> *p =NewJuqueueitem<t>(item); if(IsEmpty ()) _head= _tail =p; Else_tail->_next = p, _tail =p;}//dequeueTemplate<typename t> T juqueue<t>:: Leave () {if(IsEmpty ())ThrowEqueueempty (); Juqueueitem<T> *p =_head; T _RetVal= p->_item; _head= _head->_next; Deletep; return_RetVal;}intMain () {Juqueue<int> *p =Newjuqueue<int>;  for(inti =0; I <Ten; i++) {p-Enter (i); } std::cout<< P->leave () <<Std::endl; int*r =New int(Ten), *p =New int( -); Juqueue<int*>*t =Newjuqueue<int*>; T-Enter (R); T-Enter (q); int*s = t->Leave (); Std::cout<< *s <<Std::endl; return 0;}

C + + Learning Note 36: Class templates

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.