C ++ usage Summary (continuous update)

Source: Internet
Author: User

You can leave some time to read a book every week. Confucius said that learning is not thinking, but thinking is not learning.

Summary: A Summary of the template
1. The real parameters of multiple type parameters must be completely matched
For example: Compare (short, INT)

2. Know what a template type parameter is, what a real parameter is, and what a template non-type parameter is.

3. Explicit arguments

4. class template member functions

5. Static members of the class template

6 types of non-static function pointers

Class {
Public:
Int F (double D) {cout <D <Endl; return true ;}
};

Typedef int (A: * pfuncofa) (double D );
Pfuncofa pfunc = & A: F;

7. Suggestions:

7.1. Use of using

When a header file is to be referenced by someone else, we do not recommend using in the header file, especially "Using namespace" because after using, this will inevitably lead to a "name pollution" problem. In the header files cited by others, this pollution may affect the entire project.

The namespace itself can affect the using scope, so you can put using in the namespace. In this way, the using name only exists in this namespace; if the using name is only used in a class, you can use typedef, so it only exists in this class.

It is better not to use using, because this is often contrary to the original intention of using namespaces. When the code is read by others, you can directly see the namespace. Based on the namespace, you can better judge the functions and features of the current class ...... This also shows that the definition of the namespace name can also be considered.

 

. Protection selection of class member variables

The most obvious improvement from struct to class is the addition of "private protected public. In C ++, struct should be replaced by class, except for compatibility.

Class member variables should all be private. It is not necessary to say so, but it is absolutely reasonable. According to the object-oriented thinking, a class member is an attribute of a class. If an object's attribute changes, the object must be aware of it and carry out relevant behaviors and actions; the class attribute may be a simple variable, or it may be synthesized by several attributes. It may be simple today, but it needs to be changed to a complex one day. Therefore, it is very meaningful to use the setxxxx method to change member variables and use the getxxxx method to obtain them.

After you use get and set, you can easily track them during debugging. you can know where they are, when your class attributes are changed, and when they are read.

 

Suggestions for some small details:

1. When looping STL iterator, ++ uses ++ iterator instead of iterator ++.

2. when "Lock" is used in a function, and the "Lock" locks the entire function call, control by defining temporary variables (unlock when the temporary variables are constructed, and unlock when the structure is parsed ).

3. When the same code appears in different places, make the code into a function.

4. If an STL container needs to be synchronized, it is not only required during addition, deletion, and modification, but also during traversal.

5. When copying your own or others' Code, be sure to pay attention to useless code and comments, and the role of the entire copied code.

6. When a class is ready to define only one object, the class construction and destructor are defined as private.

8. in fact, programmers have a good attitude towards other programmers, with a very picky attitude and a learning attitude; but once they treat their own code, it is very difficult to do so; this is the most fatal. Programmers must also be picky about and learn their own code.
Assume that your code is wrong, and what you need to do is how to prove that your code is correct. Programmers themselves can do this at every stage of the Program Generation: careful design (it is worthwhile to draw time at this time, and we must ensure that we have a clear understanding of our programs.
Can begin to write), write code, unit test (unit test importance is not repeated), functional testing.

9. Throw a class exception instead of a pointer. When the stack is expanded, the memory used for local objects is released to run the local object destructor. Heap memory is not released

10. The Destructor should never throw an exception

11. For caught exceptions, call terminate to exit the program

12. Catch should be derived from minimum to maximum

13. Use auto_ptr for exception release

14. auto_ptr cannot be stored in standard containers (relationship between copying and assignment)

15. Virtual inheritance: used for multi-inheritance to implement diamond inheritance

16. Difference pointer and reference: the former can point to null, and the reference must always represent an object.

17. It is best to use the C ++ transformation operator static_cast dynamic_cast const_cast reinterpret_cast.

18. do not process the array class BST in polymorphism mode; Class bstchild: BST; FN (BST array []); when bstchild arr [10] is passed in, the compiler is misled to assume that the size of each element in the array is BST.

19. ++ -- front and back form: note that the front returns the auto-increment result, while the rear returns the old value. Therefore:

Int I = 0; cout <(I ++) <Endl; print 0;

20 Do not overload & | and, operators

21 Operator new Delete can only change the memory allocated by the compiler new, and cannot change the process of calling the constructor.

Placement New: for example, new (buffer) widget (widgetsize)

Release: PS-> ~ String (); Delete pS;

22. Do not process arrays such as: void fntest (BSTR [] arrbst) in the form of polymorphism; If BSTR is a class inheritance relationship, the object size will be caused by function calls.

23 do not provide default constructor unless necessary (write a constructor shell by yourself, can it still be exhausting)

24. Throwing an exception by referencing

Try {CT inst; throw inst ;}

Catch (CT & CTS) {} Why should we throw a reference? It is not easier to use pointers. Dizzy. Who will ensure that the objects pointed to by pointers will be parsed :? Why not pass the value? Perspiration: The two copies are ugly. The next multi-state call goes down.

25. do you know about virtual inheritance to avoid diamond inheritance: eg: B-> A, C-> A, D public B, c, dizzy, connected to, use virtual inheritance

26. Limit the number of objects produced by a class. HOHO is actually a single object. How to make a single object? The constructor is private, and a static variable is used to store the unique monomer. In fact, this is still uncomfortable. To create a single template class, you need to use a single class and directly reference it as OK. tsingal <***>...

27. smart pointer is actually a very difficult issue. STD: auto_ptr. It's nice to use it, but when you encounter a copy structure, you will find that the pointer is messy. Be careful when using it (otherwise, one pointer can only point to two different objects)

Ah, I still don't understand it very well. I heard that the smart pointer provided by the boost library is very powerful. Come back and discuss this topic later.

28. What should I do if C ++ wants to call C functions? There is a way to add an extern "C" Bay when declaring the first function. Why do we want to do this? The CPP is too strong and supports function overloading, the name of the entire function is the same. Isn't the naming rules of the function not messy. Still Be Honest With C (that is, pointers are sometimes quite disgusting)

29. if the object you copied references an external content (such as the Data allocated to the stack), let the new and old objects point to the same external content when copying the object, it is a shallow copy. If an independent copy of an external object is created for the new object when the object is copied, It is a deep copy (this is generally known)

30. The following is a summary of the use of some templates (extensive Dongdong is widely used in STL, which can be seen as useful)

The real parameters of more than 30.1 type parameters must match exactly, for example, compare (short, INT)

30.2 you know what a template type parameter is, what a real parameter is, and what a template non-type parameter is.

Template <typename T, size_t nsize> class tsingal {tsingal (T & insT) ;}// T is a template type parameter, and nsize is a non-type parameter

. Special template: This is annoying and often hard to remember. I have to remember it clearly today. Hey hey: What is template special:

Template specialization is such a definition. The actual type or actual value of one or more template parameters is specified.

The special form is as follows:

Keyword template is followed by an empty pair of angle brackets (<>). Then, the Template Name and a pair of angle brackets are followed. Angle brackets specify the template parameter specified in this special definition;

Function parameter table; function body

Template <>

Int compare <const char *> (const char * & V1, const char * & V2)

{

Return strcmp (V1, V2 );

}

What is special in the above, that is, compared with the normal comparison template, it is generally return V1> V2; (Here we compare the pointer address size), but since it becomes strcmp for comparison, the Pointer Points to the content for comparison. Oh yeah, isn't that much better. Can be freely combined.

But there is one restriction: the Special Declaration must match the corresponding template. The special nature of class templates is also a truth, so we will not write it.

31 types of non-static function pointers
Class {
Public:
Int F (double D) {cout <D <Endl; return true ;}
};

Typedef int (A: * pfuncofa) (double D );
Pfuncofa pfunc = & A: F; you can write an example by yourself. Actually, it's nothing more than referencing the class member function pointer.

32. Using: The following two opinions are raised by a boss at the headquarters.

32.1. Use of using

When a header file is to be referenced by someone else, we do not recommend using in the header file, especially "Using namespace" because after using, this will inevitably lead to a "name pollution" problem. In the header files cited by others, this pollution may affect the entire project.

The namespace itself can affect the using scope, so you can put using in the namespace. In this way, the using name only exists in this namespace; if the using name is only used in a class, you can use typedef, so it only exists in this class.

It is better not to use using, because this is often contrary to the original intention of using namespaces. When the code is read by others, you can directly see the namespace. Based on the namespace, you can better judge the functions and features of the current class ...... This also shows that the definition of the namespace name can also be considered.
 

32. 2. Protection selection of class member variables

The most obvious improvement from struct to class is the addition of "private protected public. In C ++, struct should be replaced by class, except for compatibility.

Class member variables should all be private. It is not necessary to say so, but it is absolutely reasonable. According to the object-oriented thinking, a class member is an attribute of a class. If an object's attribute changes, the object must be aware of it and carry out relevant behaviors and actions; the class attribute may be a simple variable, or it may be synthesized by several attributes. It may be simple today, but it needs to be changed to a complex one day. Therefore, it is very meaningful to use the setxxxx method to change member variables and use the getxxxx method to obtain them.

After you use get and set, you can easily track them during debugging. you can know where they are, when your class attributes are changed, and when they are read.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.