C ++ templates Study Notes (2) -- Chapter 3

Source: Internet
Author: User

1 Class Template 1.1 Class template declaration

The class template definition and implementation are put in the header file, which is similar to the function template.

Template <typename T>

Class Stack {

PRIVATE:

STD: vector <t> elems;

Public:

Stack ();

Void push (T const &);

Void POP ();

...

};

The type of this class isStack <t>, WhereTIs the template parameter. Therefore, if you want to declare your own copy constructor and value assignment operator, you should write as follows:

Template <typename T>

Class Stack {

Public:

Stack (Stack <t>Const &);

Stack <t> & operator = (stack <t> const &);

...

};

Of course, when using the class name instead of the class type, you should only useStackFor example, when you specify the class name, class constructor, and destructor, you should useStack;

 

1.2 Implementation of member functions

To define a member function of a class template, you must specify that the member function is a function template, and you also need to use the full type qualifier of this class template. Therefore, the typeStack <t>Member FunctionsPush ()The implementation is as follows.

Template <typename T>

VoidStack <t>: Push (T const & ELEM)

{

Elems. push_back (ELEM );

}

For any member function of the class template, You can implement it as an inline function and implement it in the class declaration.

 

1.3 Use of class templates

To use a class template, you must display the specified template parameters. The class template does not perform real parameter deduction, which is different from the function template.

 

Only the called member functions will be instantiated.Code. For Class templates, member functions are instantiated only when they are used. Obviously, this can save space and time. Another benefit is that you can also use this type to instantiate class templates for those types that "cannot provide all the operations in all member functions, you only need to disable the use of the template for member functions that fail to provide certain operations.

 

If a class template contains a static member, the static member will be instantiated for each type of Instantiation.

 

When using a class template as a template parameter, pay attention to the angle brackets.

Stack <stack <int>> Intstackstack;

 

1.4 Class template Specialization

Similar to function template overloading, You can optimize the implementation based on a specific type or overcome the shortcomings of a specific type when instantiating a class template. In addition, if you want to specialize in a class template, you also need to specialize in all the member functions of the class template. Although you can also only specialize in a member function, this approach does not specifically target the entire class, there is no special template for the entire class.

Special Syntax:

Template <>

Class Stack <STD: String> {

...

};

When the class template is made special, each member function must be redefined as a common function.TIt is also replaced by a specific type.

For example:

Void stack <STD: String >:: push (STD: String const & ELEM)

{

Elems. push_back (ELEM );

}

Note that before the function definition, noTemplate <STD: String>.

 

The implementation of special features can be completely different from the implementation of basic class templates.

 

1.5 Localized features

Class templates can be localized. You can specify the specific implementation of class templates in a specific environment, and some template parameters must still be defined by users.

For example:

Template <typename T1, typename T2>

Class myclass {...};

It can be specially converted:

Template <typename T, typename T>

Class myclass<T, t>{...};

 

Template <typename T>

Class myclass<T, int>{...};

 

Template <typename T1, typename T2>

Class myclass<T1 *, T2 *>{...};

 

1.6 Default template parameters

Different from the function template, the real parameters of the class template can have default values.

For exampleStack, You can have the following implementation.

Template <typename t,Typename cont = STD: vector <t>>

Class Stack {

...

};

 

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.