C + + Learning notes template

Source: Internet
Author: User
C + + Learning Notes series: http://blog.csdn.net/column/details/17301.html

1. Function templateTemplate<typename t> void name (T ...) where TypeName can also be replaced with class. (1) can be Template<class T1, Class t2> (2) template in the formal parameter name can not be unique. (3) If a template with a user-defined type is invoked, and the template uses functions or operators, then these functions and operators must be overloaded by this user-defined type, otherwise the run error (4) if a normal function and a function template are identical to the call function, then the normal function is invoked.
2. Class templateAlso called the parameter type. Template<typename T> Class A {...} use: A<int> A; (1) The definition of a member function of a class template is a function template, so the definition of a member function that appears outside the class template definition must be template, such as: Template<typename t> void A<t>::b (...) (2) All member functions of the class template must be implemented in the header file and cannot be implemented in the. cpp file. Otherwise, the function will be called with the error "unresolved external symbols"
3, non-type parameters and default type parametersNon-type parameters (available in class) Template<typename T, int i> default type parameter Template<typename t = string> (1) There is a default type parameter that can be used directly a<> A is equivalent to a<string> a (2) the default type parameter must be at the far Right (tail) of the template parameter list. When there are multiple default type parameters, one of which is not the rightmost, all parameters to the right of the parameter are ignored. Note This differs from the default argument, which does not have an error at the far right and the default type parameter does not.
4, the display of specialIf a specific user-defined type cannot use a class template or require a specific processing, it can be long defined as a realistic class template for this particular type. Template<> class A<employee> {...} new classes completely overwrite the original template, and it does not use anything from the original class template to even include different members.
5, templates and inheritanceClass templates can be derived from the class template to derive the class template can derive from the non-class template class can be derived from the class template is specific to the superclass template classes can be derived from the class template special
6, templates and friendsSuppose the template Template<typename t> class X friend 1:friend void F1 () is a friend of the x<double>,x<int>,x<...> and so on 2:frie nd void F2 (x<t> &) so F2 (X<float> &) is only a friend of x<float> (1) You can declare a member function of a class as a friend of a class template, such as: friend void A:: The F3 function of F3 () Class A is friend
7. Templates and static membersEach class template that is generated by the same class template instantiation has a copy of its own class template static data member. All objects created by a template class are shared by a static data member. Each class template special gets a copy of the static member function of its own class template.

C + + Learning Notes series: http://blog.csdn.net/column/details/17301.html

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.