C + + class template and template class in-depth detailed _c language

Source: Internet
Author: User
Tags class definition

1, in C + + template Many places have used the TypeName and class these two keywords, and sometimes the two can be replaced, then is not the two keywords exactly the same?
In fact class is used to define a class, after the template introduces C + +, the method that initially defines the template is: Template<class t> The class keyword here indicates that T is a type, and later, to avoid the possibility of confusion in the use of class in these two places, So the introduction of the TypeName keyword, which functions like class, indicates that the following symbol is a type, so that when the template is defined, it can be used as follows: Template<typename T> In the template definition syntax, the keyword class works exactly as TypeName.

2. The concept of class template and template class

(1) What is a class template

A class template (also called a generic class or Class generation Class) allows the user to define a pattern for a class that allows certain data members in the class, parameters of the default member function, and return values of some member functions to be able to take any type, including system-predefined and user-defined.
If the data type of a data member in a class is not determined, or if the type of the parameter or return value of a member function cannot be determined, the class must be declared as a template, and its presence does not represent a specific, actual class, but represents a class of classes.

(2) class template Definition

Defining a class template has two things in common:

A. First, you define the class, which is in the format:

Template <class t>
class foo
{
...
}

Foo is the class name, in the class definition body, such as a member with a universal data type, a function argument preceded by a T, where the generic type T can be the type of a normal member variable, and can also be used as a const and static member variable and as a parameter and return type of a member function. For example:

Template<class t>
class test{
private:
T N;
const T i;
static T cnt;
Public:
Test (): I (0) {}
Test (T k);
~test () {}
void print ();
T operator+ (t x);
};

B. When defining a member function outside of a class definition, if there is a template parameter in this member function, in addition to the need to define a member function outside the body of the generic class, you need to make a template declaration outside the body of the function

For example:

Template<classt>
voidtest<t>::p rint () {
std::cout<< "n=" <<n<<std::endl;
std::cout<< "i=" <<i<<std::endl;
std::cout<< "cnt=" <<cnt<<std::endl;
}

If the function is the return type with a common type, then "<T>" on the class name suffix before the function name. For example:

Template<class t>
test<t>::test (T k): I (k) {n=k;cnt++;}
Template<class t>
t test<t>::operator+ (t x) {return
n + x;
}

C. The practice of initializing Const members and static member variables outside of the class definition in vitro and the normal class in vitro initialization of Const members and static member variables is essentially the same, the only difference being that the template needs to be declared again, for example

Template<class t>
int test<t>::cnt=0;
Template<class t>
test<t>::test (T k): I (k) {n=k;cnt++;}

(3) use of class templates . The use of a class template is actually to instantiate a class template into a specific class, which is in the form of class name < actual type.
A template class is a product of a class template instantiation. Let's say an example of an image point. We compare the class template to a cookie-making mold, and template class is to use this mold made out of cookies, as to what the taste of this cookie is to see yourself in the instantiation of what the material, you can make chocolate biscuits, you can make red bean biscuits, these cookies, in addition to the material is not the same outside, Everything else is the same.

3, function templates and template functions

(1) Function template
Function templates can be used to create a generic function that supports many different formal parameters and avoids repetitive design of function bodies with overloaded functions. Its greatest feature is the data type used by the function as an argument.
The Declaration form of a function template is:

Template<typename (or Class) T>
< return type >< function name > (parameter table)
{
function body
}

Where template is the keyword that defines the template function; the angle brackets after template cannot be omitted; TypeName (or Class) is a keyword that declares the data type parameter identifier to indicate that the identifier that follows it is a data type identifier. Thus, in the function defined later, 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. For example:

Template<typename (or Class) t>
t fuc (t X, t y)
{
T x;
...
}

A function template simply declares a description of a function--a template, not a function that can be executed directly, and can produce a real function only after the actual data type of the argument is substituted for the type parameter identifier.

(2) Template function:
The generation of template functions is the process of instantiating a function template's type parameters.
For example:

Double D;
int A;
FUC (D,a);

The system replaces the T-generating function in the function template with the data type double of the argument D:

Double fuc (double x,int y)
{
double x;
...
}

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.