Template in C ++

xiaoxiao2021-03-06  42

The C template is mainly used to solve the problem of code reuse. C template has functions templates and types

Two kinds of boards.

Function template

Give you a task: write a function, deliver a pointer to the object to this function, the function

Returns the object itself, requiring this object's type, how do you write?

Is it troubled? Do we have to overload a function for all types? No

necessary. After using the function template, everything is simple, we only need to write this below:

Template

Mytype fun (mytype * a)

{

Mytype B;

B = * a;

Return B;

}

Above these statements define a function FUN, it has a mytype * type parameter, its return

The return value is MyType. The wonderful place is here, mytype represents what type is undecided, the compiler

Will be replaced by the parameter type from you to the FUN when you specifically reference this function.

The type. It feels a bit like a parameter change in the macro.

My example function writes a bit tired, mainly to explain this MYTYPE that is waiting to be replaced.

Now the function returns to the type, the function of the function and in the function body.

Once this function template is defined or called the template function, you can use it:

1:

INT A = 3, B;

B = Fun (& a);

2:

CString S1 = "abcd", S2;

S2 = FUN (& S1)

3:

Class myclass

{

.....

}

Myclass * a, b, c;

A = & b;

C = fun (a); // Requires your MyClass type to overload the assignment operator.

It can be seen that the reference template function and reference generally have no difference in grammatical form. All classes

Type replacement is a compiler quietly on the scene.

If it is to reference the template function before defining a template function, similar to the normal function,

Advantage of the original rigid statement. The format of the template function is slightly different from the normal function. Above

Example to explain that the original shape of Fun can write this:

Template

Mytype fun (mytype * a);

In fact, it is more than a Template <...> than the ordinary function.

Summary How to define a function template. First of all, the keyword template, then hosted with sharp brackets

Template parameter list. Then the same function is defined as the general function definition.

The parameter in the template parameter list is the type of use of the symbol, indicating the keyword Class, which can be used.

In the function return type, function gate type, and function.

The above example illustrates that if you want to do the same operation, then

Use a function template. If we want to do a special action to the int type, how to use functions

template? Use the above example to explain, if we want to return to the integer value for the INT type

Other types of parameters return the parameters itself, then the function template can be written this:

Template

Mytype fun (mytype * a)

{

Mytype B;

B = * a;

:: MessageBox (Null, "Other Type Parameters", "", MB_OK);

Return B;

}

/ / The above first defines a function template, and the parameter type that needs to be individually processed will be specifically defined.

Template <> fun (int * a)

{

:: Messagebox (NULL, "Plastic Parameters", "", MB_OK);

RETURN 2 * (* a);

}

Can reference the FUN function: int A = 5, b;

CHAR * CH = "abcd";

B = Fun (& a);

Fun (CH);

Compiled, you can see that the first time reference to FUN, display "integer parameters", second reference

Display "Other Type Parameters".

Class template

If you have a few classes in your program, these few classes are similar, then you can consider whether you can

Use class templates to simplify your program. example:

Template

Class myclass

{

Private:

T array [var];

PUBLIC:

T GetItem (int);

T setItem (t, int);

}

The above is the statement of the template class, the following is a statement of two member functions in the class:

Template

T Myclass :: GetItem (int N)

{

IF (n> = 0 && n}

Template

T myclass :: setITEM (t t, int N)

{

IF (n> = 0 && n}

As can be seen from the above, when a class template is declared, the beginning is the keyword template, then the pointer

Template table in parentheses. The parameters in the template parameter table are divided into two categories. The first category is the type of user.

In front of the keyword class, the second class is the variable. There are multiple types of user and variables. Take next

It is a general class declaration that you can use the types of assigns and variables in the template parameter list in the sound.

Ming.

The implementation of the members of the template class and the realization of the statement of the common class member function are almost the same.

Just starting more template keyword template and template parameter table, then in the template class name and two

There are many things that have some angle brackets.

The class template has been declared, how do you declare its object? We must explicit when declaring objects

Indicates the value of each parameter in the template parameter list. Let's declare several objects with the class template that are declared above:

Myclass OB1, * OB2;

Myclass OB3, * OB4;

Myclass OB5, * OB6;

Conceptually, MyClass is just a template, where parameters are pending. Only when all template parameters

After that, MYCLASS, MyClass and MyClass> are actually categories that can declare objects, and although they all have a common template name "M

", but because of the template parameters, they are really not the same class. So OB1, OB3,

OB5 is not the same class object, OB2, OB4, OB6 is not the same type of pointer.

Specifically, myclass is such a class: it has a 10 INT type unit

Array members, there are two member functions that read and set an integer array unit value, respectively.

Myclass is such a class: it has an array containing 20 FLOAT type units

Members, two member functions that read and set the floating point array unit value.

Myclass is such a class: it has a set of 15 cstring type units

Array members, there are two member functions that read and set the CString type array unit value.

If we use template myclass to declare an object OB:

Class Dataclass

{

.....

}

Myclass ob;

If you overload an assignment operator in the Dataclass class, then the above statement is allowed, otherwise

It is not allowed. Because the DATACLASS type will be used to assign the operator in the member function setItem.

Like template functions, we can also specify a type of template parameter type, and our classes will have a different statement different from other classes. E.g:

Template

Class myclass

{

PUBLIC:

Void show ();

}

Template

Void myclass :: show ()

{

:: MessageBox (Null, "Other Types", "", MB_OK;

}

// Declared a template class suitable for all types, which makes a special statement on the char type.

Template <> class myclass

{

PUBLIC:

Void show ();

}

Template <>

Void myclass :: show ()

{

:: MessageBox (Null, "CHAR Type", "", MB_OK);

}

Then we declare two objects with template myclass:

Myclass var1;

Myclass var2;

Myclass var3;

Var1.show ();

Var2.show ();

Var3.show ();

The result is to display "other types" twice, then display "CHAR Type". Explain that the compiler is indeed

Collapse the char types out of processing.

If you want to design an array class, it maintains an array that can increase, delete array units.

, Lookup, etc., and require the unit of this array can be any known and unknown type, how to design

? Of course, use template classes. The Carray class in the MFC is designed. Interested person can look at the file

AfXtempl.h, which is the implementation of the Carray class and the implementation of the member function.

The examples in this article are compiled in VC 6.0.

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

New Post(0)