C + + Template learning: function templates, struct-body templates, class templates

Source: Internet
Author: User

C + + templates: Functions, structs, class template implementations 1. Preface: (Know the child shoes that have the template this matter please ignore)

Common function, function overloading, template function cognition.

//The children's shoes who have studied C must have written the function sum, which was written like this:intSumintAintb) {    returnA +b;}//implements the addition of integers//if you want to achieve the addition of decimals at the same time, write a few more decimal additions. Ordinary implementation I will not write, know the function of the overloaded children's shoes will write:intSumintAintb) {//First function    returnA +b;}DoubleSumDoubleADoubleb) {//a second function    returnA +b;}//This allows us to add integers and decimals with just one sum function. //But then we're going to define two functions. //C + + considered how to avoid this repetitive operation, the code is as follows: the declaration of the function template. Template <typename t>t sum (t a,t b) {returnA +b;}//you only need to define a function and add two numbers using only one function. 
2. Examples of definitions for functions, structs, and class templates:
The function template---used to reflect: the type of the parameter passed when the function was called.
Template<class data type parameter identifier >< return type >< function name >(parameter table) {Functional Body}

Structure Template---Use embodiment: stacknode< type > S when declaring structural elements;
Template<class t>
struct Stacknode
struct T data;
struct stacknode<t> *next;

Class template---Use embodiment: stack< type > S when declaring Class object;
Template<class t>
Class Stack
T pop ();
BOOL Push (T e);
Stacknode<t> *p;
Template<class the implementation of member functions outside of the t>//class template
T Stack::p op ()

Where template is the keyword that defines a templated function; the angle brackets behind the templates cannot be omitted;class(or typename) is the keyword that declares the data type parameter identifier, which indicates that the identifier after it is a data type identifier . Thus, in a later definition of this function, any variable that wishes to determine the data type based on the actual argument type can be described with the data type parameter identifier, so that the variable can be adapted to different data types.

The basic idea of 3. template

① when you find a set of operations on multiple different types of variable operations. For example, we want to implement the chain stack in this class, where the elements can be int char.

When the ② is implemented, the type of the head pointer of the chain stack will be the corresponding int node, char node ... The corresponding pointer type.

③ then copy and paste the int to char so there are two classes of Stack_int Stack_char.

④, of course, no problem. But the whole class implements down the number of rows is many. When we look at so many more lines just int char different duplicate code, it is also easy to write wrong.

The ⑤ template is the implementation of the code in front of the class definition and the Outer class member function with Template<class t> PS. No semicolon, only T can change.

⑥ then the whole place int char is changed to T ... Change the class name to stack.

⑦ that's how it's made. A template for Stack_int and Stack_char

⑧ before declaring the object is {Stack_int A;  Stack_char b;} Now it's {stack<int> A; Stack<char> b;}

Perfect!!! Feeling templates Simply simplify multiple repetitive codes into a single .... O.o ... But it really works ...

4. Questions to be aware of in use:

The ① function template allows you to use multiple type parameters, but you must have the keyword TypeName or class before each parameter in the template definition section, that is:

template<class data type parameter identifier 1, ...,class data type parameter identifier n>< return type >< function name >(parameter table) {Functional Body}

② does not allow different statements between the template statement and the function template definition statement < return type >. The following declaration is incorrect:

template<class t>int  I; T min (t x,t y) {function body}

③ template functions are similar to overloaded functions, but they are quite different: when you overload a function, you can perform different actions within each function body, but template functions that are instantiated by the same function template must perform the same action.

④ a function that declares a template function "similar" between a function template and a template function call, and this parameter has an implicit conversion!

5. Code of Study:
1Template <classT>//Structure Template2 structnode3 {    4 T data; 5          structNode<t> *Lchild; 6          structNode<t> *Rchild;7 };8Template <classT>//in-Class class templates9 classbinTen { One Private:     ANode <T>h; -  Public:     -          voidpri (); the }; -Template <classT>//Out-of- class member function implementation template - voidBin<t>::p ri () - {     +Node<t> *p =Newnode;  -cout<<"Qunimade"<<Endl; + } A int_tmain (intARGC, _tchar*argv[]) at { -bin<int>ABC; -}

2017-03-11 19:16:23-2017-03-22 22:51:24

C + + Template learning: function templates, struct-body templates, 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.