The interpretation of C + + template functions and template classes

Source: Internet
Author: User

Template (Templates)

I. Function template

In C + +, we can write a generic function that uses a non-instantiated parameter type, which is instantiated when the function is called. This function is called a function template.

We used to implement a function of the time will write a separate method to achieve, that is, all the code is written in the main function, there is no package and module, such a programming style is not very good, not enough to implement code reuse.

Eg:int Iarray[max] = {Ten, +, +, 50};float Farray[max] = {1.1, 1.2, 1.3, 1.4, 1.5};char *carray[max] = {"One", "one", "Three", "four", "Five"};sx:for (int i =0;i<max;i++) {cout<<iarray[i]<< "";} for (int i =0;i<max;i++) {cout<<farray[i]<< "";} for (int i =0;i<max;i++) {cout<<carray[i]<< "";}

Another implementation is the initial reuse of code, is to write an output function array output;

void PrintArray (int* array,int size); for (int i =0;i<max;i++) {cout<<iarray[i]<< "";} PrintArray (Array,max); void PrintArray (float* array,int size); void PrintArray (char** array,int size);

We can see that this output function behaves the same, except that the output type of the operation is different.


Now we use the function template to write a generic function to output the operation by line.

First, let's look at the difference between a normal function and a function template:

1. Normal function: The parameter value is undefined, but the parameter type is OK. 2. Function templates: Not only is the value of the parameter indeterminate, the type of the even parameter is not deterministic, which is a more advanced reuse.


Template<class t> ——— This is the declaration of the function template

T is a type parameter, and T is a type that is not instantiated. T may be int,float,char* ....

void PrintArray (t* array,int size) {for (int i =0;i<size;i++) {cout<<array[i];}}

When a function template is called, the template is automatically instantiated, and the type of the template parameter depends on the type of argument passed in.

Main:printarray (Iarray,max);    Here T is instantiated as an int type printArray (Farray,max);    Here T is instantiated as float type PrintArray (Carray,max); Here T is instantiated as a char type


Summary: function template is the function of the same function as a template, so as to facilitate unified management.


Two. Class template

We can see from the function template that the parameters of the function template are different; Here we may think of many of the classes that we have touched before, these classes are different parameters, here we can write a basic class template, when an object declaration, we need to give a definite type.

Function templates: A common function that can manipulate multiple types.

Class templates: Only the member variable types are different, but the operations (member functions) of these member variables are similar.

The Declaration and implementation of the class template must be done in the. h file.


Template <class t>,t is a type, unknown type

t* stackptr; The template T declaration member function is used here, and after being instantiated with int, it can be treated as all T in the class declaration is replaced with the instantiated int type.

Each member function of a template class is a template function, and when implemented, each member function is preceded by a template declaration.

Template<class t>stack<t>::stack (int ...) Initializes the constructor of the class,


Main

Unlike automatic instantiation of function templates, function templates are instantiated at the time of invocation. The class template must, however, explicitly instantiate the template with the specified type when declaring the template's object.

Stack<int> Stack; Instantiate the template t as an int type.

Template parameters can also have default values.

Template<class T = The default value of the int> template parameter, which is the specific data type.

Template parameters can have more than one.

(1) Template<class T,class s>; (2) Template<class T,int max_size>

T is a parameterized type. Max_size is a non-type parameter, the template parameter of the type has been determined, and the non-type parameter can only be integral, typically used for array subscripts.




This article is from the "Wodenianshao Technology blog" blog, make sure to keep this source http://wodenianshao.blog.51cto.com/10604330/1686856

The interpretation of C + + template functions and template classes

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.