C + + Learning notes (2)--function template template , class template detailed (1) __jquery

Source: Internet
Author: User
Tags function definition
(i) Concept of function template 1--Suppose you want to write a function to sum two parameters.
		   In actual programming, we might want to define a few such functions, each of which can sum up a given type of value, and then it might be natural to think of using overloaded functions. For example: int add (int a,int b) {return a+b;
		   Double Add (double a,double b) {return a+b;
		   } char Add (char A,char b) {return a+b; 2--These functions are almost identical, the function bodies of each function are the same, functionality is the same, and the only difference between them is that the parameter type and function return value types are different 3--in fact, we have to write all of the code in the code, so we use the functions of copying, pasting, modifying and so on.
		To get a sum function of another type.
		4--However, if each type requires a duplicate function type and function body, this is cumbersome and error prone.
	5--More importantly, we need to know in advance which types we might support. If you want to use a function for an unknown type, there is a problem with this method 1--c++ has--template mechanism (in Java, generic mechanism can be solved), can use-function template-to solve the above problems.
The 2--function template (functional template)----is a type-independent function that is not related to the type, and is automatically instantiated only when needed, thus creating a "batch type" programming method. (ii) Definition of function template and use of 1 function templates The syntax form defined by the function template is: template< template parameter list > function return value type function name (form parameter list) {function Body} first line, called template definition , which is then called the template function 1--template definition begins with the keyword template, followed by the template parameter list 2--template parameter list (template parameterlist)---is a pair of angle brackets <>
		   The enclosed one or more template parameter tables-----------are not allowed to be separated by commas between empty parameters, there are two forms: 1--the first---typename parameter 2 .... 2--The second kind---CLass parameter 1,class parameter 2 ....
		   The 3--template definition is followed by a function definition in which the type parameters in the template parameter list can be used, for example: Template<typename t> Add (T a,t a b) {return a+b;
	The 4--function template defines the meaning of the syntax---is a generic function, the function type and the parameter type are not specified, but are represented by a type tag, which is determined by the compiler-based on the function used, this---universal function---is called---function template
		
		2 The function template can be used like a normal function, using the call of the template function. The code looks as follows: #include <iostream> using namespace std;
		Template<typename t>t Add (T a,t b) {return a+b;
			int main () {cout<< "Int--add" <<add (10,20) <<endl;
			cout<< "Double--add" <<add (10.2,20.5) <<endl;
	
			cout<< "Char--add" <<add (a,a) <<endl;
			System ("pause");
		return 0; (iii) Advanced features of function templates (1) non-type parameter 1--template parameters do not have to be all-types, you can use non-type parameters (Nontype parameter), in general form: Template<typename t,int n> V
		OID Outchar (T a) {for (int i=1;i<=n;i++) cout<<a;
2--when calling a function, the untyped parameter must explicitly give its value, in the general form: Outchar (char,5) (' A '); *****************************************************************************Class template details (i) Definition of class template 1--as you can define---function templates, you can also define---class templates, which are defined in the general form of: template< Template parameter list >class class template name {1--constructor 2--destructor 3--member function 4--data member} 2--class template--must begin with--keyword template--, followed by template parameter list <template The parameter list> 3--template parameter---is enclosed in a pair of <> angle brackets, is not allowed to be empty, and the formal parameters are separated by commas 4--because-class templates--contain--type arguments--thus, also called---parameterized classes 5-- If---class is an abstraction of an object, object is a---instance of a class, the class template is an abstraction of the class, and the class is an instance of the template, which can be used to create classes that support various data types--------equivalent to generics in Java, for example, define a class template that represents a point on a plane: template 6-- <class t> class point{public://"1" default constructor point (): X (0), y (0) {}//"2" constructor point with arguments (cons 
				 T T a,const t B): X (a), Y (b) {}//"3" member function void Set (t a,t b);
				 void Display () {cout<< "x:" <<x<< "Y" <<y<<endl;
		} private:t X,y;
};
		(b) The definition of Class template object: 1--with class template--when defining objects, the specified type arguments must be displayed for--template parameters, in general form: class template name < type real parameter table > object name Class table;
		Class template name < type argument Table > object name 1 (argument list 1), Object Name 2 (argument list 2) ...               For example: Point<int> a,b; Define the class template object and invoke the default constructor Point<doubLe e>m (1,2), n (3,4); To define a class template object and invoke the parameter constructor

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.