Fame

zhaozj2021-02-12  215

Fame

There is an old saying in China: the name is not constant. The same thing, if the name is not good, not only sound uncomfortable, but also the true nature is also easy to cover. If there is a good name, we can more easily remember it and understand it.

In modern Chinese, many words have followed, such as the two words we often see below.

Ice is smart.

Smart ice.

Ice and snow is intelligent, and she is smart like Ice.

Smart ice and snow emphasizes the ice, she is very smart, it looks also even more ice-like exquisite purity.

There are several such terms in C , but we used to use it in a lot of time, almost replaced with each other. Below I want to completely distinguish a few terms so that you can avoid a lot of conceptual confusion and use errors.

These words are:

Function pointer - pointer function

Array pointer - array of pointers

Class template - template class

Function template - template function

In the end, we can make them the name, the name is smooth.

1. Function pointer - pointer function

The focus of the function pointer is the pointer. Indicates a pointer, which pointing is a function, example:

INT (* pf) ();

The focus of the pointer function is the function. Indicates a function, its return value is a pointer. example:

INT * fun ();

2. A number of set pointers - pointer arrays

The focus of array pointers is the pointer. Indicates a pointer, it points to an array, example:

INT (* pa) [8];

The focus of the array of pointers is an array. Indicates an array that the element it contains is a pointer. example;

INT * AP [8];

3. Class Template - Template Class (Class Template - Template Class)

The focus of class template is template. Indicates a template that is specifically used to generate a mold. example:

Template

Class vector

{

...

}

Use this Vector template to produce a lot of Class (classes), Vector , vector , vector >, vector ....

The focus of the template class is class. Indicates the class generated by a template. example:

The above Vector , Vector , ... all is a template class.

These two words are easily confused, I see that many articles use it wrong, even some English articles are like this. It is important to distinguish between them, you can also understand why the header file of the template is. H, the member function implementation of the template must also be written in the header file. Can't be like a normal class (Class) , The Class declaration is written in .h file, the Definition is written in the .cpp file. Please refer to Marshall Cline in [34] Conta Lite "in" C FAQ Lite "in [34] why can't i Separate The Definition and Put IT I Separate The Definition and Put IT ISIDE A.CPP FILE? URL address Is http://www.parashift.com/containers-and-templates.html#faq-34.12

I will take a few key paragraphs as follows, English is very good:

In order for the compiler to generate the code, it must see both the template definition (not just declaration) and the specific types / whatever used to "fill in" the template. For example, if you're trying to use a Foo < INT>, The Compiler Must See Both the foo timeing to make a specific foo .

Suppose you have a template foo defined Like this:

Template class foo {public: foo (); Void SomeMethod (T x); private: T x;};

Along with Similar Definitions for the Member Functions:

Template foo :: foo () {...} template void foo :: SomeMethod (t x) {...}

Now suppose you have some code in file bar.cpp That Uses foo :

// bar.cpp void blah_blah_blah () {... foo f; f.somethod (5); ...}

Clearly somebody somewhere is going to have to use the "pattern" for the constructor definition and for the someMethod () definition and instantiate those when T is actually int. But if you had put the definition of the constructor and someMethod () into file Foo .CPP, the Compiler Would See the Template Code When IT Compiled Foo.cpp And It Would See Foo When IT Compiled Bar.cpp, But There Wild Never Be A Time When It Saw Both The Template Code and foo . SO BY Rule Above, IT Could Never Generate The Code for Foo :: someMethod ().

An example of a default template parameter:

Template

Class Array

{

...

}

For the first time I defined this template and use it, it is useful:

Array Books; // I think there is a default template parameter, which is equivalent to array books

The above usage is wrong, the compile will not pass, because Array is not a class. The correct usage is array <> books;

Here Array <> is a specific class generated by a class template for default template parameters.

4. Function Template - The focus of the function template function template is template. Indicates a template, specifically used to produce functions. example:

Template

Void Fun (T A)

{

...

}

When using it, you can explicitly produce a template function, Fun , Fun , Fun ...

You can also use the compiler to be derived from the compiler during use, help you imply Implicitly.

Fun (6); // Implicit Generate Fun

Fun (8.9); // Implicit Generate Fun

Fun ('a'); // Implicit Generate Fun

Shape * ps = new CIRLCLE;

Fun (PS); // Implicit Generate Fun

The focus of the template function is a function. Indicates a function generated by a template. example:

Fun generated above explicitly or implicitly, Fun ... is a template function.

About the template itself is a very huge theme. To make it clear, it is not an article, but a book, fortunate, this book already has: David Vandevoorde, Nicolai M. Josuttis Written "C Templates: The Complete Guide". Unfortunately, I can't buy the paper version in the mainland, but there is an electronic version to circulate online.

The use of the template itself is limited, in general, they are just a mold that generates class and functions. In addition, the field of use is very small, so it is impossible to have any template pointer, that is, the pointer to the template, because in C , the template is a code code production tool, in the final code, At all, there is no template itself, and only the specific classes and specific functions of the template are exist.

However, the class template can also be used as a template template parameter (the policy-based design "policy-based design in the" Modern C Design "in Andrei Alexandrescu.

Template Class Y>

Class foo

{

...

}

From the discussion of the article, it can be seen that the name is very important. If the use of the name is inappropriate, it will cause a lot of trouble and misunderstandings. Our naming of various identifiers in the actual procedure is also a knowledge, in order to understand, sometimes it needs to pay a certain price.

Finally, in several terms in this article, the focus of the language is behind, the previous words are used as adjectives.

Wu Tong writes in 2003.6.1

Recently modified 2003.6.16

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

New Post(0)