C + + Learning notes

Source: Internet
Author: User

Http://www.cnblogs.com/maowang1991/p/3290321.html

The following content for their more than a year of C + + learning experience, pure original, reproduced please indicate the source address.

More than a year of C + + learning process, I read a lot of C + + classics, there is "effective C + +", "more effective C + +", "C + + primer" and so on, each reading works will summarize some of the experience, now take out and share with you.

The default access method for 1.STRUCT members is public, and class default access is private! 2.exit function Termination program execution calls destructors, Abort function terminator does not call destructors! 3. Static local variables do not exit until the program terminates! 4. A reference to the private member returned through the public function might break the encapsulation of the class, causing the external variable to change the class private member value! 5. A const object can only call a constant member function, and a const member function may have a very overloaded version overload! 6. A const data member can be initialized only at the time of definition or in a constructor with a member initialization value, and cannot be initialized with an assignment statement! 7. To use Delete in the destructor to free the memory space used by the new request! 8. Writing destructors to free up the memory space requested by members of the class and using deep copy functions is good programming habit! 9.operator++ () is quite similar to ++d,operator++ (int) is equivalent to d++! 10. If a function in the parent class is a virtual function, it is good programming practice to explicitly declare the virtual function in each subclass! 11. If the class is a base class, its destructor is declared as a virtual destructor so that the derived class can call its own destructor! 12. A new one corresponds to a delete is a good programming habit! The 13.istream input read is read from the file, and the Ostream output write is written to the file! 14.istream is seekg reset file pointer position, Ostream is SEEKP, file with file.eof () to determine whether to read the end of the file! 15.assert (condition), when the condition is met does not assert, does not meet the conditions will be assert! 16. For variables that do not need to be modified, using a constant reference to pass parameters can guarantee both security and efficiency! 17. Call Srand (Time (0) before the loop, and then call the rand () function in the loop to make the results different at random! 18. One-dimensional array parameters are as follows: Int a[], the two-dimensional array parameter is as follows: Int a[][size], the first [] is empty, the back must be non-empty! #define A 10 There is no semicolon behind! 20. The variable created with new in the local function is in memory, even if the local function completes the memory variable still exists, but the pointer to it may be a local variable, you need to call Delete before the end of the local function to free memory space to avoid memory leaks 21. Array declarations are best declared with T *a , it's not easy to make mistakes, and creating objects is best done with new instead of directly! 22. When defining a class, destructors (freeing resources) and copy constructors(Deep copy) best display definition! 23.c/c++ language input terminator is CTRL + Z (under Windows)! printf for 24.C language ("%.9lf", a); More accurate than C + + 's setprecision (10), C + + sometimes auto-rounding accuracy! 25. Clarify the situation, logic rigorous, variable initialization, whether or not, the boundary judgment is correct! 26. To make the base element in the stack a template class type, you must define some of the functions of the template class: default constructor, friend output function, etc. 27. Replace global variables with single-piece mode and global function for easy expansion and maintenance! 28. Use the const char *PTR to indicate that the variable the PTR points to is a constant. char* Const PTR indicates that PTR itself is a good programming habit for constants! 29.const int *i = &a, but the representative cannot be modified by the I pointer, but can be modified by other means, such as a=3! 30. Be flexible and proficient in language, such as (A&LT;B?A:B&LT;C?B:C) = Val (); This sentence merges 3 if statements into a single sentence! 31. Bridging mode bridge enables separation of interfaces and implementations, which reduces the range of classes that need to be modified when modifying classes! 32. Priority: I++ is higher than ++i, note that the operator precedence looks at the following operator precedence table! 33. Recursive algorithm in line with the idea of the approximate algorithm, do not have to delve into thinking, reasonable can! 34. Using the Null in C + + to initialize a pointer can guarantee that the numeric literal constant 0 can be converted to any one of the pointer types corresponding to the empty pointer! 35. Non-const objects can call const and non-const functions, but const objects only call the Const function! 36. You can use a non-const type variable to assign a const type argument, and you cannot assign a const type variable to a non-const type parameter! 37. Class overloads the two-tuple operator, either as an intrinsic with only one argument to the class, or as a friend function with two arguments in the class! 38. The built-in operator-> is two yuan, and the overloaded operator-> is a unary, if the overloaded operator-> first call the overloaded, until repeated calls to the built-in operator-> to terminate! 39. Using STL as much as possible is a good programming habit! 40.++i returns a memory variable that can be either an lvalue or an rvalue, while i++ returns a literal, not a memory, and cannot be left-hand-valued! The continue does not occur in a 41.for loop, but the use of continue in the while is prone to a dead loop becauseCan I no self-added! 42. Take the maximum priority analysis principle will take the longest type, so the righteousness:list<vector<string> > Lovos, the right two >> must have a space between, if not, the compiler may be interpreted as >> Right shift operator! 43. For ordinary integer constants, you can use the enumeration amount to represent! 44. Assigning a derived class to a base class is not to use the object to continue assigning values, but to use pointers or references to avoid truncation! 45. Mediation types that do not use void * For type conversions are good programming practices because void * Erases pointer type information! 46. Using reference types or standard library components to avoid the complexity of introducing multi-level pointers is a better design! 47. Avoid using level two pointers in class objects, especially when the base class pointers to derived class objects! 48. In the C + + class, try not to overload the type conversion operator function, but instead use a function that has a clear meaning such as toint (), ToChar (), and so on! In 49.c++, it is sometimes possible to suppress implicit type conversions by adding the explicit keyword to the class constructor to reduce many errors! 50. Multi-use reference passing parameters, with the value of the transfer will have a large number of replication operations, inefficient, multi-use reference is good programming habits! 51. A function object is a class object that overloads the operator (), and using a function object to generate an anonymous temporary object is a good programming habit! 52. Use Dynamic_cast<>,static_cast<>,const_cast<>,reinterpret_cast<>! 53. Use the member initialization list as much as possible in the derived class constructor, and static data members and arrays are not allowed to initialize in the member initialization table and can be initialized in the body of the function! 54. In classes involving pointers and references, it is best to show the compose deep copy constructor and assignment operator Overload operator= () to avoid errors! 55. Subclasses when inheriting a parent class if overriding a virtual or non-virtual function of the parent class, ensure that the function access modifier and the parent class that are overridden in the subclass are consistent with the access modifiers obtained after the inherited access! 56. When writing the copy constructor and overriding the assignment operator function, all variables in the class must be displayed with the ' = ' sign, do not write part, if you do not write, the compiler will only call the default constructor for variable initialization, and will not default to the ' = ' number! 57. Try not to use static variables in the class, never do the static initialization of the run time! 58. Direct initialization of class objects or entities that may be class objects instead of initialization with assignment statements, such as: n n(0)! 59. To understand the differences between copy initialization and assignment initialization, copy initialization is called copy constructor implementation, and assignment is overloaded ' = ' implementation! 60. You can suppress the replication initialization of a class by declaring the copy constructor in the private zone! 61. The object as a parameter as far as possible to use reference to pass, and then consider pointers, try not to pass value, if you do not want to modify the object can be added const! 62. Throw anonymous temporary exception objects and catch them as a reference, and if they need to be re-thrown, throw an existing exception object, instead of handling the original exception, throw a new Exception object! 63. Do not return a reference to the storage allocated within the scope of the function (return pointer or direct value can), causing a memory leak! 64. Use the constructor and destructor of the class to encapsulate some paired operations such as locking and unlock, so that the class object is created at the beginning of the function execution, using a A; Instead of a A (), or a () to create, before the end of the function will automatically delete the object, the implementation of the start call lock operation, the end of the call unlock operation, commonly known as RAII (Resource acquisition is initialized)! 65. Copy the Auto_ptr template instantiation object to transfer control of the object to, and then set the copy source to null! 66. Never use the Auto_ptr instantiated type as the basic type of the STL container! 67. Do not pass the Auto_ptr object as a function parameter by value, emptying the original object and causing unpredictable errors! 68. Do not provide the default initializer for the parameter in the virtual function! 69. The access level (public protected private) has no effect on overriding virtual functions, even if public inherits, you can also write the inherited functions that are supposed to be public to the private scope, completely! 70. Overrides or overloads of a non-virtual function in a base class in a derived class are obscured, and all virtual and non-virtual functions with the same name in the base class are obscured, making them invalid, and when you want to make an overload, you must use the using A::fun () function to import the fun function in the base class! 71. Overloads must be overloaded under the same class scope, or they are only obscured! 72. Overloaded virtual functions either rewrite each of the base classes or either do not rewrite or use the Using A::fun ()! Try to avoid using pointers in 73.c++, but use references! 74. The copy operation is not implemented through the template member function, it must show the implementation of the copy operation! 75. The user-defined version of the post-form self-increment decrement operator should return a constant, because the post-post cannot be left as the value! 76. The front-mounted type is always better than the rear-mounted type, i.e. ++i is always better than i++! 77. Non-members allow type conversions to be performed on the first parameter, while membersA function cannot perform a type conversion on the first argument! 78. When the pointer is re-copied, be sure to be aware of the possibility of a memory leak, whether you want to release the current pointer memory space! 79.mutable variable can be modified in the Const function, its effect is better than const_cast<&gt, so try to use mutable! 80. Class overloading operators sometimes have to have arguments and functions that are const, which ensures that even passing constant arguments is not a problem when other function calls! 81. The variable is passed to the constant parameter, but the constant is passed to the variable parameter but not! 82. Use virtual functions to achieve different functions, the base class as small as possible into a smaller base class! 83. Use dynamic binding instead of conditional dispatch to implement type-related requirements! 84. If a type is likely to be a base class, write it as an abstract base class from the beginning! 85. The base class inherited by public should generally be abstract and should focus on interface inheritance rather than code reuse! 86. The interface class is no data member, the destructor is declared as virtual function, the ordinary member function is pure virtual function, does not declare the type of constructor! 87. Either public or friend function, other function cross-calls such as protected inherited may be problematic! 88. Try not to use class arrays, use STL containers, such as vectors, and container types are pointers rather than objects, such as Vector<b *>! 89.wget W1 = W2; Call copy constructor, wget w1;w1 = W2; The class assignment operator is called! 90. Class parameter pass-through is called copy constructor, more const reference! 91.ENUM{STAR=5}; The star defined here can be used as a constant literal, and the # define Star 5 effect is the same! 92. Use Const,enum,inline instead of # define, use the template inline function instead of macros! 93. For simple constants, it is best to use const and enum instead of # define, for macros that resemble functions, it is best to replace them with the inline function! 94. If Const appears on the left side of the reference is a constant, if the const appears on the right, then the pointer is a constant! The const vector<int>::iterator in 95.STL equals t*const,vector<int>::const_iterator equivalent to a const t*! 96. The const version of the function can be overloaded with the original version, and a const function called with a non-const function can reduce code duplication! 97. Using FunctionsReturns a static object that converts a non-local static object into a Locat static object! 98. The assigned value of the reference replaces the value of the object referred to by the alias on the right side of the equals sign, or 2 objects! 99. You can define an abstract class by declaring a pure virtual destructor, at which point the pure virtual destructor must be defined. 100. Destructors should never spit out an exception, even if an exception occurs in the destructor inside the processing, either end or swallow! 101. Do not call the virtual function in constructors and destructors, not down to the derived class class, with an up-pass parameter substitution! 102. Make the assignment operator return a reference to *this! 103. Copy constructors and assignment symbols in derived classes do not automatically copy or assign a base class part, so the call base class copy or Assignment function is displayed! 104. When freeing the memory space, the original pointer refers to the memory area to back up, then delete or point to the new memory space, and then delete the original backup area! 105. Use tr1::shared_ptr and auto_ptr smart pointers to automatically free up memory space! 106. Try not to define a type conversion symbol in a class, even if the definition does not have a return type, because the type converter itself already specifies the return type! 107.shared_ptr object If there are multiple pointers to a pointer, only the first one needs to be initialized with a pointer, the other is either with the copy constructor, or with the assignment operator initialization, the pointer initialization can only be used once! 108. For built-in types, STL, and function objects, the values are more efficient, but for other types, it is more efficient to pass constant references! 109. References and pointers support polymorphism, because the inside of the reference is done by pointers! 110. Replace the member function with the Non-member and Non-friend functions as much as possible! 111. If a function is related to a class, but not a member function, then the function does not necessarily have to be a friend function, and if the function can be done through the public interface of the class, it does not have to be declared as a friend function, but the return value of the function is generally const! 112. The constructor can be implicitly type-cast if it is not explicit, regardless of the constructor's arguments, but only if you give the actual number of arguments less than the number of required parameters of the constructor, you must ensure that the constructor has a default constructor parameter! 113. An in-class object intrinsic function can access the object's own private member variable, but if there is another object of the same kind as the function's argument, the intrinsic function can also access the private member variable of the other object! 114. As far as possible to define the occurrence of variables, it is best to wait until the variable needs to be used and the initial value is also determined when the definition! 115. Try to avoid using transformations, even if you have to use C + + specific!116 such as static_cast. Virtual destructors are public and there are no other types like private and protected! 117. According to the actual need to select the mode, is is-a or has-a,private inheritance and has-a similar, but generally will choose has-a! 118. Because private inheritance is not a is-a relationship, it is not possible to satisfy the case of a base class pointer to a derived class, and of course not to use polymorphism, only as with Has-a! The 119.virtual function, even if it is a private function and is inherited by private, is inaccessible to derived classes, but the derived class can inherit the base class virtual function and override it without affecting it! 120. The pointer can change the object it points to, but the reference does not, once the reference is assigned cannot be changed! 121. Overloading an operator should return a reference instead of a pointer! 122. The implicit conversion can only be converted once, more than once is forbidden! 123. Prefix return reference, suffix returns const value, suffix is implemented by prefix, prefix efficiency is higher than suffix! 124. Dispose of the resources in the destructor, use references to catch exceptions instead of passing values or pointers! 125. Template and exception specifications do not mix, exception specification is a should be carefully used features! 126.STL for iterator try to use (*iterator). First to take the value, not to use----to value! 127.c++ prevents temporary variables from being created for a very variable reference, so implicit type conversions do not occur when you pass parameters with a very variable reference! 128. A constant reference creates a temporary variable, and an implicit type conversion occurs! 129. Each overloaded operator must have at least one parameter of the user-defined type! 130. The precedence of the operator is higher than *!131. Each species has a vtbl, and each object has a Vptr,rtti that adds an item to the VTBL, so it only increases the class size without increasing the size of the object! 132. destructors must be defined! 133.auto_ptr objects are best passed with a const reference, not by value! 134. The compiler can only help you do one type conversion by default, more than once! 135. Never provide a conversion operator to a smart pointer that is converted to the original pointer type! 136. When using type conversion too much trouble, you might as well use the template bar! 137. Be sure to provide a deep copy constructor for classes that contain pointers to avoid errors in future use! 138. Constructors and destructors can inherit from the quilt class, but if the destructor is a virtual function, the followingThe destructor of the bearing subclass is also a virtual function by default, but you'd better add more readability with virtual! 139.cout<<setiosflags (ios::fixed) <<setprecision (n) <<s1<<endl; Used to set the number of digits in C + + after the decimal point! 140. Use extern "C" to change the function to the C function style, that is, the obj file cannot be changed by name! 141. You can use the right-shift operation and the &1 operation by dividing the decimal into binary or judging whether it is divisible by 2 or divided by 2! In 142.c++ and C, assigning integers to character variables is the assignment of integers to character variables as ASCII codes, assigning character variables to integers and assigning ASCII codes to integers for character variables! The STL container in 143.c++ works by the built-in type is a bit copy, the custom object is a copy of the object, the use of insert or push_back is a copy, if the object is created in the heap, then the pointer to the STL, if in the stack, the object itself is passed! When using STL containers in 144.c++, if the object copy action is more, the base class also records the derived class, then holds the pointer, if the basic type or rarely copies, then holds the object!

I spent a lot of time to organize the above experience, I hope to learn C + + to help you.

C + + Learning notes (RPM)

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.