Template beginner guide

xiaoxiao2021-03-06  41

Template Beginner Guide Original: http://www.codeproject.com/cpp/templates_part1.asp

When developing large applications, for different functions and classes, you can save a lot of time by using a shared code template. The template is independent of the data in a common function or a class. In this guide, I will process template functions and template classes. Suppose you have implemented a class handling stack, and this stack class can handle the value of the Double type of PUSH POP read status. What should I do if I need a constitutive stack class? Without template technology, you have to copy this stack class code. Such efficiency is not high. With templates, you can define a template class or a function, use all functions and types, you can declare a new variable in the template definition. See how you work below:

Function template

Suppose we need a function template to find minimum values ​​in different types:

Template EleMType Calcmin (ELMTYPE ELEMFIELD [INT IMIN = 0; for (INT i = 1; i

This is the function template. He looks forward to a data type and will return one of them. Use this template to see the following example:

Void lettstestthefunctionTemplate () {int ifield [] = {1, 2, 3, 4, 5, 6}; double dfield [] = {2.5, 2.31, 10.23, 15.2};

Int isize1 = sizeof (iField) / sizeof (int); int I = calcmin (ifield, isize1); int isize2 = sizeof (dfield) / sizeof (double); Double D = Calcmin (Dfield, ISize2);}

Template MIN is used by two different data types. One is int [], and the Double [], but the function is functional. Find minimal and return minimum.

Function templates can also use Inline, Extern Static declaration. Note To put these Template keywords and parameters. as follows:

Template

Inline ElemType Swap (ElemType & A, ElemType & B);

Class template

Define class templates Similar definition function templates. Look at the example below, the general STACK class handles different types. The class is defined as follows:

Template

Class Stack

{

PUBLIC:

Stack ();

~ Stack ();

Void Push (Const ElemType & Anelement);

Void Pop (ElemType & Anelement);

Bool Waserror () Const;

Bool isempty () const;

Private:

ELMTYPE ELEMS [ISIZE];

INT ITOP;

Bool Berroroccd;

In addition to some symbols, this type of implementation is not much different from the usual implementation. When defining a class template, you can use a normal class. But you must specify parameters in . In the template, the class name can be used without parameters. See the implementation of the following:

// incrude your prototype here or use a #define

Template

Stack :: stack ()

: ITOP (0), Berroroccd (False)

{

}

Template

Stack :: ~ stack ()

{

}

Template

Void Stack :: Push (const elemtype & anelement)

{

Berroroccd = (iTOP == isize);

IF (! Berroroccd)

ELEMS [ITOP ] = AneElement;

}

Template

Void Stack :: POP (ElemType & Anelement)

{

Berroroccd = (iTOP == 0);

IF (! Berroroccd)

Anelement = Elems [- ITOP];

}

Template

Bool Stack :: WasError () Const

{

Return Berroroccd;

}

Template

Bool Stack :: isempty () Const

{

Return (ITOP == 0);

}

Use the class template as follows:

Stack itheintstack;

Stack DTHEDOUBLESTACK;

Overload function template

Function template

Function templates can be used by other function templates or other functions. The compiler will traverse all possible function templates and will create the corresponding template function. Find the use of the optimal matching strategy.

Use friends and other templates in the template

The template class can contain other templates or class, or other classes can also be used as friends. When a template class contains additional classes, there are two possibilities:

The internal class can be a usual class. The internal class is independent of the template parameters. Otherwise the internal class is another template. The external template class contains another independent template (also independent of template parameters).

Template

Class Tree

{

// ...

PUBLIC:

Class node

{

Friend Tree ;

// ...

}

}

In this example, the internal template class Node is independent of Tree. The external class is defined as Node's friend, contains a list of parameters.

Template type

When using the type defined in the template parameter, you should use Typename to define:

Template

Class X

{

// ...

TypeName T :: x twwhestuff; //t :: x is the type // ...

}

Class test

{

// ...

Class x {/ * ... * /};

}

Do not use TypeName, compiler error.

Enumeration template

When using an enumerated template, you can create a generic class that generates an object. Provide an enumeration function to allocate memory. This enumeration function can be implemented using a template enumeration function. You can use any type:

Class Builder

{

// ...

Template static t * allocateMem ();

}

Note: Template enumeration functions cannot be Virtual.

End

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

New Post(0)