Concept of C ++ (2)

xiaoxiao2021-03-06  141

Many newcomers are particularly prone to doubts, confused. I feel that I have learned this thing, I spent so much time, will it be used? This kind of thought is very common.

In some forums, they often see the pros and disadvantages of each language, knowing that the language they have learned is good, but if they stay at this level, they are not used. Any language is just a tool. It is important to use the tool! As far as my personal experience, people who really develop in the industry are willing to use mature, to complete their work. And novices like to use some new technologies to do development, and like to pursue novel (easy to see from the interface, the flower green interface is mostly from the newcomer). In fact, there are some efficient methods that may be low, scalable, and maintenance methods to solve the problem is not the development of the developer. They are just working in work. But as a truly self-motivated developer, we should use more elegant and efficient programming technology, which is a good habit we gradually become "big cows". Old is staying in place, it is easy to be eliminated. This is especially true in software development. Whether it is a student, or a first-line developer, I don't think I can't produce a "book reading enough"! I sometimes like to turn out the book I have seen, and I can always understand some new things every time. This is something like C language book, and I think I haven't been very good. why? Because I think the learning language is only the first step in the newcomer to enter the software to develop "Hell". The language is far less than enough. It is also necessary to learn related libraries (C of course first choice first learning C standard libraries), Related platform technologies (such as .NET), more far, and exercise the analysis, summary ability of the target problem. Before working, the technical route is the master, after work, most of the programmers will be around the company's technology route. So, I have time now, you can learn some you are interested. If you want to engage in software development, especially the development of power system software, learn C will not disappoint us. When we entered the front door of C , then after a dark road, then from the back door to the top of the light, we will feel the feeling of "a small mountain".

Recommended books:

"Programming Master Proverbs" ---------- Liang Yixin (using super-tie, should know, ^ _ ^) Write the first book, which is still worth reading. In this book, Liang tells us that learning is to be patient, we must resist "loneliness", take your own way, let others go to "say"!

Recently, I am busy. It turns out that the topic is about the details of some specific C languages, but it still can't take a large period of time. So now I can only talk about some stories about C .

C originates from the C language, remember to learn the time to learn C language for a long time (that is a happy and full time), but now learn C , not on the basis of C, if this is, we think, we are It is not good to play C . Therefore, C is never upgraded or expanded, we should learn C as a new language (C parent Bjarne Stroustrup ".

Write programs first hope that the program can be executed correctly, followed by the efficiency to be accepted, and again is easy to maintain. C is a difficult language. C provides too much option, and uses C to write programs to have four think modes: process, object-oriented and generic. We use a language to write a program, which does not mean that use the language itself. In other words, we are more when we use the library in writing programs. For example, MFC, STL, ATL, VCL, and more. Among them, C is used to write a beautiful, excellent performance, simple code, easy-to-maintain code, and push the C standard library. STL has a strict requirement for efficiency, and the procedure written by STL is simple and beautiful (I specially posted a post on several integers for some integer. In fact, the purpose is to showcase STL's simple and elegant). Once you are used to using generic thinking, we can fully understand the beauty of the template! For numerical calculations, the C standard library can fully meet the requirements of the modern service and commercial calculations on data and information. I think that I have a good language is to practice. That is, "Write"! "Accumulation of Engineering Experience" is very important for programmers that have been developed! Only in constant accumulation, we can gradually understand some of the things behind the C language. For this point, there is no rookie with a lot of program code writing experience, or you can use "Effective C " first with some experience. "Effective C " is a good book! . Meyers' book is absolutely worth reading, and Meyers can say today's C community has a technical expert.

Recommended website:

Www.royaloo.com

The following text should be doodled last year, mainly about dynamic memory allocation, here to look at it, think it is too light. Because the memory is the "sports field" running, the degree of understanding of the site will directly affect the fluency and stability of our program run.

C provides operator New to allocate memory on the heap, operator delete to release memory. In some cases, we need to better control the allocation and release of memory. Many programs create and release a large number of important objects, such as Tree Nodes, Linked Lists Links, Points, Lines, Messages, etc. Using universal memory distributors such as New and Delete to make these object allocation and release sometimes dominate The runtime and memory requirements of the program.

Two-aspect of factors: General Memory Distribution Operations The cost of operation and space consumption and memory fragments caused by different object sizes. Class Use custom memory splitters will speed up the speed of execution of the simulator, compiler, and similar programs.

Exception a case where better memory control is: a program that needs to operate for a long period of time for a limited resource. Real-time systems often require minimal cost to obtain guaranteed expected memory. This also leads to better memory control. In general, these programs avoid using dynamic memory allocation, and use special purpose memory distributors to manage limited resources.

In addition, there are also cases where the hardware or system requirements need to be placed in the specified memory location. This also needs customized memory management (implemented by overloading New).

In C Release 2.0, in order to meet the above needs, the memory management mechanism has made corresponding modifications. Mainly introduced Operator New [] and Operator Delete [].

Scope of the new operator (Scope for Operator New Functions)

Operator Range :: Operator New Global

Class-name :: Operator new class

The first parameter of the operator new must be type size_t (the type defined in stddef.h), the return type void *.

When allocating an built-in type object, the class object of the user-defined new operator function is not included, and the global new operator function is used when any type of array is used. When the NEW operator is customized in the class, the NEW operator of the class is called when the memory of the class object is assigned. as follows:

#include

#include

Class Blanks

{

PUBLIC:

Blanks () {}

Void * Operator New (size_t stallocateblock, char chinit);

}

Void * Blanks :: Operator New (size_t stallocateblock, char chinit)

{

Void * pvTemp = malloc (stallocateblock);

IF (pvTemp! = 0)

MEMSET (PVTemp, Chinit, StallocateBlock);

Return PVTEMP;

}

int main ()

{

Blanks * a5 = new (0xA5) Blanks; // Create an object Blanks, and initial testing is 0xA5

Return A5! = 0;

}

The New operator can be overloaded, while Delete is not. Because when you need to release it, what we can get is a pointer. Moreover, this pointer may not be the original object type pointer (possibly a type conversion). In fact, when a pointer to a single memory is obtained using the NEW, there is an indicator (INDICATOR) before the slice memory, and the actual number of memory is recorded. When the DELETE is called, you can know the memory size you need to release. Dialocating arrays: DEAALLOCATING ARRAYS:

Void f ()

{

X * p1 = new x [10];

// ...

DELETE [] X;

}

Why don't you use Delete [10] X; free memory? Bjarne Stroustrup said that this approach is easy to cause errors, and the task of the number of records is placed in the implementation of Delete.

As for why C , the C Language COLLECTION is seen, "C Language Design and Evolution" (EN) Bjarne Stroustrup Machinery Industry Press (commonly known as: D & E) can get an answer.

In addition, a smart pointer Auto_PTR is provided in the C standard library. This pointer can help us prevent "resource leakage when you are thrown out." However, the disadvantage is that the smart pointer cannot point to an array because its internal release memory is performed by delete instead of delete []. So, you can only use it to point to a single object.

The template portion is a more difficult part in C , and it is also the charm of C . The following text is what I have seen before, and the specific source is unclear. Today, I finished taking it, as a unit introduced as a template.

Why use templates

For except types, the other functions (such as Quicksort), we generally have three solutions.

1. Repeat the function entity (C language approach) for each different type (C language approach):

INT * quicksort (int A []) {...}

Double * quicksort (double a []) {...} ...

2, use Object (Java practice) or void *

Disadvantage

There is also a problem in terms of efficiency issues

Type check

3, use the macro pretreatment mechanism

Disadvantages:

Just stupid text replacement, and will not consider scope and type security.

However, the application template can avoid these disadvantages, we can write:

Template

T * quicksort (t a []) {...}

advantage:

The code is simple and elegant, all parameter types are replaced by T, and the type irrelevant is truly realized. Better type security, all types of checks are performed during the compile period, and the pointer is avoided. There is no inheritance, high efficiency. (1) There is no virtual function; (2) All all work is completed during the compile period and greatly improve the operational efficiency.

purpose:

Tell the compiler how to make the best choice, and this choice is completed in the compile period.

Template mechanism:

Charge and instructors

1, specialization

Basic template:

Template

Class a {// (1)

Void F (T1 A, T2 B);

}

Local specialization (offset):

Template Class a {// (2)

Void F (Int A, T2 B);

}

or

Template> Class a {// (3)

Void f (t a, t b);

}

Global Specialization (Explicit Specialization):

Template <>

Class a {

Void F (int A, int b); // (4)

}

Use example:

A * p1; // will be used (4) - global specialization

A * p2; // will be used (3) - local specialization

A * p3; // will be used (2) - local specialization

A * p4; // will be generated by (1) - basic template

// a

advantage:

By: Global Special Type -> Local Charge -> Basic Template, the selection and matching of this specialization order (overload analysis rules) is automatically carried out by the compiler without manual participation. Different implementations can be given according to different situations (such as different types, different conditions), resulting in more flexible targeted.

The scalability of the program can be improved in response to any change.

2 inscription

T Const & F (T Const & A, T Const & B)

{

Return A B; // 1

}

INT g = f (1, 2);

In fact, F (1,2) to match the function is int CONST & F (Int Const &, Int Const &);

And what is this function?

advantage:

There is no need to provide a pair of angle brackets and inside, such as F (1, 2), with real paramers, we can write into f (1, 2).

Template application

1, standard library (STL) - template code everywhere

Standard library = algorithm container iterator

List /

2, type irrelevant (T)

3, Trait and Policy

(1) Trait: It mainly uses many typedef and specialization, specifying a feature.

// Traits / AccuMtraits3.HPP

Template

Lass.

c template <> class account accounts {

PUBLIC:

TypedEf int ACCT;

Static acct const ZERO = 0;

}

Template <>

Class AccumulationTraits {

PUBLIC:

TypedEf int ACCT;

Static acct const ZERO = 0;

}

Template <>

Class AccumulationTraits {

PUBLIC:

TypedEf long ACCT;

Static acct const ZERO = 0;

}

(2) Policy: Usually manifested as a function, specifying a behavior

Class SUMPOLICY {

PUBLIC:

Template

Static Void Accumulate (T1 & Total, T2 Const & Value) {

Total = Value;

}

}

(3) Trait and policy usage:

Template>

Class Accum {

PUBLIC:

Typedf TypeName Traits :: acct acct;

Static Acct Accum (T Const * Beg, T Const * End) {

Acct total = traits :: zero ();

While (beg! = End) {

Policy :: Accumulate (Total, * Beg);

Beg;

}

Return Total;

}

}

4, Metaprogramming

Compile calculation, recursive ideology

5, new form design template

(Third, fourth, fifth, more detailed description)

Specific introduction to "C Templates Chinese"

Part 1 introduces the basic concepts of the template, introducing these basic concepts in the style of tutorial.

Part 2 describes the language details of the template, which can be used as a reference manual based on a template.

Part 3 describes the basic design technologies supported by the C template, and the range covers the concept of tiny to complex usage; some technologies have not appeared in other books.

Part 4 is based on the first two parts, in-depth discussion of a common application of various types of templates.

How long does it take to learn VC?

For a month, you can write some small programs you can play with VC.

Learn two months, you can use the vc to write like something in front of the people.

Three months, you can start with the boss with VC.

Learn for six months, began to go to learn C

After a year, I decided to continue, if IF (continue) Learn MFC, ATL, STL, C #, BCB, NetWork, DataBA

SE, Algorithm ... Else is an error

After three years, learn how to solve problems in programming languages, VC, BCB, etc. are just a tool for solving problems. At this moment

If you are still studying C , you can work in software development this very "future" occupation.

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

New Post(0)