6 after the interview-differences between template function templates and template class templates

Source: Internet
Author: User

1. Remember an instance
Template <class type parameter table>
Return type function name (parameter table)
{// Function Definition body}

Template <class T> T min (t x, t y );

////////////////////////////////
# Include <iostream. h>
Template <class T>/* function template declaration */
T min (t x, t y)
{If (x <Y) return X;
Else return y ;}
Void main ()
{Int n1 = 2, n2 = 10;
Double d1 = 1.5, D2 = 5.6;
Cout <"small integer:" <min (N1, N2) <Endl; // compares two integers in the instantiated min Template Function
Cout <"smaller real number:" <min (D1, D2) <Endl; // compare the two Double Precision values by instantiating the min Template Function
}
Function template min (x, y)
Template Function min (N1, N2) int type
Template Function min (D1, D2) Double Type

Class Template
Template <class type parameter table>
Class class name {
// Class definition ......
};

For example, define a class template:
Template <classt1, classt2>
Class myclass {T1 I ;//
T2 J ;//
Public:
Myclass (T1 A, T2 B )//
{I = A; j = B ;}
Void show ()
{Cout <"I =" <"J =" <j <Endl ;}};
If the main function defines a template class myclass <int, double> and declares a Class Object ob1 (1 Reference statement: myclass <int, double> OB (2, 0.1 ); note: myclass <int, double> instantiates the class template, that is, T1 instance is of the int type, T2 is of the double type, so we get a template class. then you can define the Class Object ob1 and initialize it.
You can also define another template class, such as myclass <double, char>.
Indicates the relationship between a class template and a template class.
Class template myclass <t !, T2>
Template Class myclass (INT, double)
Template Class nyclass <double, char>
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.
 

Http://blog.csdn.net/rufeng18/archive/2007/12/01/1910461.aspx

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.