Template Beginner Guide 2

zhaozj2021-02-17  56

Template Beginner Guide 2 Original: http://www.codeproject.com/cpp/templates_part1.asp 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-30041.html

New Post(0)