Customization of C ++ template: Partial custom C ++ class

zhaozj2021-02-11  178

Customization of C template: Partial custom C class

The multi-fragages of the class template is as follows:

Template

Class classname

{

......

}

T can be any type. In the previous section, we introduced custom class templates. This section said that the part of the type template is customized, meaning only part of the template parameter, so that we want the purpose, this section directly describes it. Syntax, I want to believe everyone is not willing to know about its grammar directly. Let's take a look at the example of a class template part.

The prototype of the SpecTemplate class is as follows:

Template

Class SpecTemplate

{

PUBLIC:

SpecTemplate (t t, p): _ Valt (t), _ valp (p) {}

~ SpecTemplate () {}

T typet () {return_valt;

Void Typet (T & T) {_valt = t;

Void Display () {cout << _ Valt << endl;}

......

Private:

T_valt;

P_valp;

}

Suppose we want to customize the template parameter P of the SpecTemplate class. When P is the String type, we want its corresponding SpecTemplate class and the universal Spectemplate class to have different features. At this time, you will be partially customized, and some customizations are as follows:

Template

Class SpecTemplate

{

PUBLIC:

SpecTemplate (t t, int p): _ Valt (t), _ Valp (p) {}

~ SpecTemplate () {}

T typet () {return_valt;

Void Typet (T & T) {_valt = t;

Void Display () {cout << _ valp << endl;

......

Private:

T_valt;

INT_VALP;

}

This completes the customization of the class template, thereby achieving your special requirements. Here is just examples, please don't say something like this. I only hope that everyone can understand the usage.

Note: This writing is now not supported, but this is a C standard writing, and if you want to try a BC or GCC compiler.

The rush of the article, there is a typison or error, please pay more. Thank you for your trip, I am grateful, welcome to communicate with you (---- Yuan Kai -----).

转载请注明原文地址:https://www.9cbs.com/read-5598.html

New Post(0)