C ++: Template (ambiguous... Take a look)

Source: Internet
Author: User
Tags float max
C ++: Template

1. The concept of a template.

Reason for introducing the template:

We have learned how to reload functions. for heavy-duty functions, the C ++ checking mechanism can vary with function parameters and classes. Call the overload function correctly. For example, to obtain the maximum values of two numbers, we define the max () function to define different overloaded versions for different data types.

Int max (int x, int y );
{Return (x> Y )? X: Y;
}
Float max (float X, float y ){
Return (x> Y )? X: Y ;}
Double max (Double X, Double Y)
{Return (C> Y )? X: Y ;}

However, in the main function, we define char A and B respectively;

An error occurs when running max (a, B); because we have not defined an overloaded version of the char type.

Now, let's re-examine the above max () functions. They all have the same function, that is, to find the maximum value of two numbers. Can we write only one set of code to solve this problem? This will avoid calling errors caused by incomplete definition of overload functions. To solve the above problem, C ++ introduces a template mechanism. template definition: A template is a tool for implementing code reuse mechanisms. It can implement type parameterization, that is, define the type as a parameter, this achieves real code reusability. Template category: After the grinding board is divided into function templates, when the compilation system finds a corresponding function call, based on the type of the real parameter, determine whether to match the corresponding form parameter in the function template and generate an overload function, which is called the template function. The difference between a function template and a template function: the difference between the two can be analogous to the difference between a class and an object. Function templates and classes are similar to template definitions, while template functions are similar to objects. Is an instance of the function template with program code. Memory usage. After a class template is described, you can create an instance of the class template to generate a template class. The difference between a class template and a template class is that a class template is a template definition. Instead of a real class, a template class is a real class.

2. Events related to function templates and templates

The general life form of the function template is as follows:

Template
Return type function name (parameter table)
{// Function Definition body}

Note: templarte is a keyword of the Declaration template, indicating that the class of the Declaration template keyword cannot be omitted. If the type parameter is redundant, class <type parameter table> must be added before each parameter. It can contain basic data types and class types.

See the following program:

# Include
Template /* Function template declaration */
T min (t x, t y)
{If (x
Else return y ;}
Void main ()
{Int n1 = 2, n2 = 10;
Double d1 = 1.5, D2 = 5.6;
Cout <"small integer:" <min (N1, N2) <
Cout <"smaller real number:" <
}

Program running result: small integer: 2
Smaller number: 1.2

Program analysis: the main () function defines two integer variables N1 and N2, D1 and D2, and then calls min (N1, N2 ); that is, the instantiation function template T min (t x, t y) where T is int type, find the minimum value in N1, N2. similarly, when min (D1, D2) is called, the minimum value in D1 and D2 is obtained.

It indicates the function template instantiation process.

Function template min (x, y)

Template Function min (N1, N2) int type

Template Function min (D1, D2) Double Type

If a cout is added to the main () function <

The program will encounter errors because the parameters of the template function T must be completely consistent with each other and do not have the implicit type conversion function.

Iii. Category templates and templates

1. Define a class template:

Template
Class class name {
// Class definition ......
};

Template declares the keywords of each template, indicating that a template can be declared. template parameters can be one or multiple, but they should be abstract results, it should not be a specific (such as Int or float) type, a member function parameter or a return type. The preceding parameter type must be added.

For example, define a class template:
Template
Class myclass {T1 I ;//
T2 J ;//
Public:
Myclass (T1 A, T2 B )//
{I = A; j = B ;}
Void show ()
{Cout <"I =" <"J =" <

If a template class myclass is defined in the main function And declare a Class Object ob1 (1 Reference statement: myclass Ob (2, 0.1); Note: myclass The class template is instantiated, that is, T1 instance is of the int type, T2 is of the double type, so we get a template class. Then we can define the Class Object ob1 and initialize it.
You can also define another template class, such as myclass.

Indicates the relationship between a class template and a template class.

Class template myclass
Template Class myclass (INT, double)
Template Class nyclass
Conclusion: A function template is a type of function abstraction that represents a type of function. This type of function has the same function and represents a specific function that can be called by class objects, function templates cannot be called by class objects.
A class template is an abstraction of a class that represents a class. These classes have the same functions, but the data member types and member function return types and parameter types are different. A template class is an instance of a template class. represents a specific class. class objects can be defined, but objects cannot be defined for 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.