Functional programming-Template functions _ Class templates

Source: Internet
Author: User

function business logic , just different types of function parameters
The nature of function templates: type parameterization -generic programming

Grammar:

Template <typename t><class T1,class t2> multiple parameter type type function name (formal parameter table) {statement sequence;}

Function Template Basics:

Template is to tell the C + + compiler to start generic programming, see T, don't give an error

 template <typename t>//  a template   void  Myswap (t& A, T& b) {T C;  C  = A; A  = b;}  //  when invoked, explicitly describes the type  myswap<int  > (x, y); Myswap  <char  > (x, y);  //   automatic type down, try not to use  myswap (x, y), template  <typename T1, TypeName T2>  //  two templates, defined must be used   void  Myswap (t1& A, T2& b) {}  

When you use a template, you will need to clear the original schema and recompile the content when you modify the template.

Function templates encounter function overloading:

Function templates do not allow automatic type conversions, strict type matching
The normal function can be automatic type conversion, without losing the accuracy of the premise
1. Function templates can be overloaded like normal functions
2. C + + compiler takes precedence over common functions
3, if the function template can produce a better match, then select the template (such as the use of ordinary functions resulting in the absence of precision)
4. The syntax of an empty template argument list can be qualified by the compiler to match only through templates

Template <typename t>//a templatevoidMyswap (t& A, t&b)  {T C; C=A; A=b;}//a common functionvoidMyswap (int& A,Char&b) {intC; C=A; A=b;}intA =Ten;Charc ='C'; Myswap (a,c)//Calling normal functionsMyswap (C,a)//calling normal functions for stealth type conversionsMyswap (A,a)//call template functions, strict type matching, no type conversion, but normal function if there are two int parameters, precedence function, look at an example

template function overloading
intMaxintAintb) {returnA>b?a:b;}
Template<typename t>T Max (t A, T b) {returnA>b?a:b;}
Template<typename t>T Max (t A, T B, t C) {returnMax (A, B), c);}intA =1;intb =2; Max (A, b); // function templates and normal functions are satisfied when calling, precedence common function Max<> (A, b); // show using the function template method, use the <> null type parameterized list Max3.0,4.0);//type is consistent, you can use templates. and use normal function, distortion, then call function templateMax3.0,4.0,5.0); // function templates can be overloaded Max'a', -);//Type inconsistent, cannot use function template, call normal function for implicit type conversion

function template Mechanism Conclusion:

The compiler does not treat function templates as functions that can handle any class.
function templates produce different functions through specific types
The compiler compiles the function template two times:
Compile the template code itself where it is declared , and compile the code after the replacement of the parameter where it is called

Class Template:
Problem Solving: Multiple classes are functionally consistent and data types are different.
Class template is used to implement the type parameterization of the data required by the class;
Class templates are particularly important in representing arrays, tables, graphs and other data structures;
implement the separation of data structure and algorithm , the class template can keep the list type data of different types
Definition and use of class templates:

Template <typename t>classa{ Public: A (T a=0) { This->a =A; } Public:voidPrintA () {cout<<"A:"<< a <<Endl; }protected: T A;}
intMain () {A<int> A1 (Ten);//template classes are abstract and need to be type materialized    A1.printa (); System ("Pause"); return 0;}

The class template does the function parameter:

void Usea (a<int//C + + compiler requires specific parameter class {B.printa ();} Usea (A1);

Class templates in Inheritance:

First, the template class derived from the General class
When a child template class is derived, the template class needs to be materialized, and the C + + compiler needs to know what the data type of the parent class is specifically
To know how much memory the parent class occupies, only the data type is fixed to know how to allocate memory

classB: Publica<int>{ Public: B (intA=Ten,intb= -):a<int> (a)//Initializes the parent class object with the  initialization parameter list, noting that when the parent class is a template class, the A<int> class type  is used to materialize    { This->b =b; }protected:Private:intb;}

B B1 (1,2);

Second, template class derived template class

Template <typename t>classC: PublicA<t>//base class is template type{ Public: C (t C, t a): a<T> (a)//base class is template type    { This->c =C; }voidPrintc () {cout<<"C1:"<< C <<Endl;}Private: T C;}
voidMain () {C<int&GT;C1 (1,2); C1.printc (); System ("Pause");}

function overloading of template classes:

The friend function is used only for overloaded input and output streams << and >>
The rest use member functions, the member functions need to be declared in the class, and << needs to be declared in the Ostream class, but we have a lot of trouble in C + + source code.
Therefore, the UF meta-realization. The friend function only needs to define ostream as the friend function of the class in the class where it needs to be used.

Methods for writing friend functions and member function overloads in a template class:
1. All function declaration implementations are written inside the. h file (simple)
2, Declaration and implementation separate, but all in one. cpp
When the function is presented, the parameter type, the class scope, the return value note the use of <T>
Friend function: only for input and output streams

operator  out //friend function Declaration of <T> out-of-class implementation: Template<typename t>ostreamoperatorout// A friend function is a global function that does not require the scope of a class {}

Template<typename t>
member functions:

operator // It's okay to write normally. Out-of- class definition: main three elements Complex<T> complex<t>::operator+ (complex<t>& C2) {Complex  TEM (a+c2.a,b+c2.b);  return  tem;}

In addition to the input and output streams using the Friend function overload, the rest of the best uses member function overloading, otherwise it is cumbersome to handle ...

3.h and. HPP separate, other classes used, to include. HPP (not. cpp)
This time is similar to the situation 2, the misuse of the friend function is prone to problems.

Static keywords in template classes, different invocation types, static keywords belong to different classes, which are determined by the implementation mechanism of the template.

Static is a specific class.

Functional programming-Template functions _ Class templates

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.