C + + Programming Method 4: Class template

Source: Internet
Author: User

Class template

When defining a class, you can also abstract some types and replace them with template parameters, making the class more versatile. This class is called a template class, for example:

Template <typename t> class A

{

T data;

Public
How class template member functions are defined one: defined in a class template

void print () {cout << data << Endl;}

}

How class template member functions are defined two: outside the class template

template<typename t> void A::p rint () {cout <<data << Endl;}

Class Templates (2)

After class templates instantiate a class, the class is instantiated as an object

Class template

Template parameters for the class template

1. Type parameters: Using TypeName or class tags

2. Non-type parameter: Integer, enumeration, pointer (to object or function), reference (Reference object or reference function). Where the integer type is more commonly used, such as:

Template<typename t,unsigned size>

Class Array

{

T Elems[size];

...

}

Array<char,10> array0;//defining an object with a class template instance

Template parameter is another class template

The corresponding declaration form is as follows:

Template<typename t,template<typename tt0,typename tt1> class a>

struct FOO

{

A<t,t> Bar;

};

C + + Programming Method 4: 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.