C++ advanced cold knowledge you don't know

 

  1. When the return value of the function template is uncertain, you can refer to the new C++11 post-declaration and stop to declare: templat<class T1,class T2> auto gt(T1 x,T2 y)->decltype(x+ y){ return x+y;}
  2. The functions defined in the class declaration automatically become inline functions . If you want to explicitly turn a member function into an inline function, you only need to add the inline keyword before the function definition
  3. The constructor with only one parameter allows to initialize it using assignment syntax, if you want to prohibit its implicit conversion, you can use the explicit keyword to modify
  4. The scheme of initializing the object array is: first use the default constructor to create the array element, then the constructor in the curly braces will create a temporary object, and then finally copy the temporary object to the corresponding element, so if you want to create an array of class objects, this The class must have a default constructor.
  5. C++11 provides a new enumeration whose scope is class: enum class :short egg{Small,Media,Large};
  6. The derived class object can be directly assigned to the base class, but the content only involves the base class part
  7. In the case of diamond inheritance, it is forbidden to pass information to the grandparent class through the parent class. Therefore, when constructing the subclass, the parent class will not call the grandparent class constructor, but only call its default constructor. To call the parameterized constructor of the grandparent class, you need to bring the parameterized constructor of the grandparent class when the subclass is declared.
  8. Any two positive numbers that are less than the int type are added, and they will be promoted to the int type before being added. ----Integer promotion
  9. There are global variables and static variables in the two files respectively. When static variables are used in the global variable files, because it is impossible to confirm whether the static variables and initialization are completed at this time, the result is also uncertain
  10. C++ only allows const automatic type conversion of a layer of pointers
  11. New failure will not return a null pointer, only an exception will be thrown. The only way to make it return a null pointer is to add (std::nothrow) after new, for example int* p = new (std::nothrow) int;
  12. STL array allocates memory on the stack, vector allocates on the heap, pair has no memory management, where to apply and where to allocate
  13. STL's remove_if algorithm does not delete elements, but only moves the elements to be deleted to the end
  14. The remove_copy_if in the STL algorithm needs to apply for elements for the container before use
  15. Noexcept just tells the compiler that this function will not throw exceptions, and it will not handle exceptions, thereby improving efficiency . However, whether an exception is actually thrown is up to the developer to decide. If noexcept is declared, the result is thrown. , The compiler will not process it, and the program will exit directly
  16. If noexcept is used in the move constructor of a class, move copy will be used when expanding the container containing this class. Otherwise, copy copy will be used. Note that the difference will only be reflected in the expansion .
  17. Reference variables of const type can only be initialized in the initialization list of the class
  18. Static member variables can only be initialized outside the class, but member variables of the static const type can be initialized within the class
  19. It is not possible to use static and const (prefix modification) to modify member functions at the same time , because when the C++ compiler implements a const function, in order to ensure that the function does not modify the members of the object, an implicit parameter is passed in: const this * , But the static function does not have this pointer, which means that static and const are in conflict.
  20. When you don’t want a class to be inherited or a virtual function to be rewritten, you can add a final keyword after the class name or virtual function name to modify it to indicate that it cannot be rewritten. If it is rewritten by mistake, when compiling, The compiler will report an error
  21. Initialization sequence: (1) static variables of the base class (2) static variables of the derived class (3) member variables of the base class (4) parent class constructor (5) member variable of the derived class (6) derived class constructor
  22. What does it mean when * and & modify formal parameters at the same time in C++, for example, int insert (int *& head): Originally passed in *head, it means that the formal parameter head is a pointer and can only change the content it points to, but through & After modification, the pointer head can also be modified.
  23. Which functions cannot be virtual functions: (1) global ordinary functions (2) friend functions (3) constructors (4) inline member functions (5) static member functions
  24. It is not possible to declare two virtual member functions and static member functions with the same name and the same parameters in a class, and the compiler is confused
  25. Virtual functions cannot be inlined, because inline functions are determined at compile time, but virtual functions are determined at runtime. Even if the inline keyword is added before the virtual function, it will still be ignored
  26. Functions with const modification and without const modification are actually two functions, so a virtual function in the parent class is added with const, but the subclass member function with the same name and the same parameter is not added with const and will not be overwritten
  27. The const_iterator in STL refers to the content pointed to by the iterator cannot be modified, but the iterator can perform ++ operations
  28. You cannot directly assign an array to another array, nor does a string array, but you can directly assign a structure to another structure
  29. Arrays can be initialized in braces and the equal sign can be omitted, but this can only be used during initialization, such as int[12]{0}; it means that the first element is initialized to 0, and the rest of the elements use default values, that is, Is 0
  30. Both cin.getline(str,size) and cin.get(str,size) indicate that the entire line of string is read until a newline character or size bytes is read, but cin.getline(str,size) is reading When the newline character is taken away and replaced with'\n' instead of the line character, cin.get(str,size) will leave the newline character in the queue. At this time, its variant cin.get() can be used Read this byte. Note that the input parameter command: cin>>str; will also leave the newline character in the queue
  31. The prefix u, U, L, and R of the string represent char16_t, char32_t, wchar_t and the original string respectively
  32. C++ requires that enumeration variables can only be assigned with enumeration values
  33. A delete or free operation on a null pointer will do nothing
  34. The array name is a pointer constant, the address of the first element of the array, and the address of the array name is the entire array
  35. Prefix self-addition is more efficient than suffix self-addition, because the prefix directly returns the result of adding 1 to its value, while the suffix will first copy a copy, then add 1 to the copy, and then return the copy. Of course there is no difference between C++ built-in types (int, double)
  36. Cin will ignore newline characters and space characters, and you can use cin.get() to read them
  37. Character function library is cctype
  38. When using a reference as a formal parameter, the actual parameter type must be consistent with the type of the formal parameter reference, such as func(int& i). At this time, if a variable of type long is passed in, an error will be reported, and the passed must be an lvalue, unless the shape Participated in the const modification.
  39. If the function uses a const reference as a formal parameter, and when the function is called, the input is a constant or does not match the const reference type of the formal parameter, C++ will create a temporary anonymous variable to store the value
  40. When a function uses a reference as a formal parameter, try to use const modification. The advantages are as follows: (1) It can avoid the compilation error of inadvertently modifying the data (2) Using const reference can make the function accept const and non-const arguments, otherwise it can only accept non-const. const (3) Use const reference to make the function correctly generate and use temporary variables
  41. The type reference and the type itself are treated as the same signature. Whether const can be used as a function overload signature depends on the situation https://www.cnblogs.com/qingergege/p/7609533.html
  42. Function template: display materialization: template<>void Swap<Job>(Job& j1,Job& j2); display materialization: template void Swap<Job>(Job& j1,Job& j2); Note: display materialization does not include <> , You can use the function to create a display instantiation: Swap<Job>(j1,j2);
  43. The calling sequence of function overloading: (1) Create a list of candidate functions, that is, all functions with the same function name, including function templates (2) Filter out functions with matching parameters, including default parameters (3) Best match: exact match , A. Conventional function is better than template b, promotion conversion, namely int->long c, user-defined conversion
  44. If you don’t know the variable type in the function template, you can use decltype to modify it, which will generate an accurate type based on the actual parameter type passed in, for example: decltype(expression) var; The specific judgment logic is as follows: (1) If expression does not use parentheses Enclosed, the type is exactly the same as the expression. For example, const int i =0; decltype(i) var; then var is const int. (2) If expression is a function call, var is the same as the return value of the function. Note that the function will not be called at this time. The compiler obtains the return type through the function prototype (3) If expression is an lvalue and is enclosed in parentheses, var is a reference to its type. For example, int i =0; decltype((i)) var; then var is int&, (4) the above conditions are not satisfied, then var is exactly the same as expression. For example, long i =0; decltype(i+6) var; this When var is a long type.

Guess you like

Origin blog.csdn.net/Chiang2018/article/details/113829577