Some Usage skills of the C ++ template class

Source: Internet
Author: User
Moved to: http://www.wypblog.com/archives/184

C ++ allows you to specify the type parameter in the template class as a charming type. For example, you can assign int to the type parameter T in the general class Stack as the default type, as shown below:

Template <class T = int> class Stack {// other operator };

Now we can use the default type to declare the template class object like the following code:

Stack <> stack; // store int value

However, you must note that the default type can only be used for template classes, but not for template functions.

In the template prefix, in addition to type parameters, you can also use non-type parameters. For example, in the Stack class, you can declare the array capacity as a parameter, as shown below;

Template <class T, int capacity> class Stack {... private: T elements [capacity]; int size ;};

In this way, when creating a stack, in addition to specifying the element type, it also specifies the size of the array. For example:

Stack <int, 100> stack;

This statement declares a stack that can contain up to 100 int values.

Reprinted Please note: Reprinted from past memories (http://www.wypblog.com /)
Link: some tips for using the C ++ template class (http://www.wypblog.com/archives/184)

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.