Introduction to C + + Classic-Example 9.3-Class template, simple class template

Source: Internet
Author: User
Tags class definition

1: Use the template keyword to define not only function templates, but also class templates. A class template represents a race, which is a mechanism used to describe a common data type or processing method, which enables the parameters or return values of some data members and member functions in a class to take arbitrary data types. Class templates can be said to generate classes with classes, reducing the definition and number of classes.

2: The general definition of a class template is as follows:

Template < type form parameter table > class templates name

{

...//class template body

}

The class template member functions are defined in the following form:

Template < type form parameter table >

return type class template name < type name table;:: member function name (formal argument list)

{

...//function body

}

Where template is the keyword, the type-form parameter table is the same as the definition of the function template. The class template's member functions are defined when the class template reputation class template is defined to be consistent. Class template is not really a class, you need to regenerate the class, generating the form of tired as follows:

Class template name < type actual parameter table >

The newly generated class definition object is in the following form:

Class template name < type actual parameter table > object name

Where the type argument table should match the type-form parameter table in the class template. Classes generated with class templates are called template classes. Class templates and template classes are not the same concept, the class template is the definition of the template, not the real class, the definition to use the type parameter, the template class is essentially the same as the normal class, which is the class template after the type parameter instantiation to get the class.

3: A type parameter table in a class template can be specified at execution time, or it can be specified when a class template is defined. The instance code is as follows:

//9.3.cpp: Defines the entry point of the console application. //#include"stdafx.h"#include<iostream>using namespacestd;template<classT1,classT2>classmytemplate{T1 t1;    T2 T2;  Public: MyTemplate (T1 tt1,t2 tt2) {T1=TT1, t2=Tt2;} voiddisplay () {cout<< T1 <<' '<< T2 <<Endl;}};voidMain () {intA=123; Doubleb=3.1415; MyTemplate<int,Double>MT (A, b); Mt.display ();}
View Code

Operation Result:

Introduction to C + + Classic-Example 9.3-Class template, simple class template

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.