Generic and object-oriented C ++

Source: Internet
Author: User

Generic and object-oriented C ++

1. the default function defined in the class is inline. The inline function should be defined in the header file because it must be visible to the compiler, this allows the compiler to inline expand the code of the function at the call point. At this time, only the function prototype is not enough.

2. assert

3. Exceptions

4. because the stream object cannot be copied, it cannot be stored in the container. Because the stream cannot be copied, the form parameter or return type cannot be the stream type. You must use a pointer or reference, the read/write operation on the IO object changes its status. Therefore, the reference must be non-const.

5. If you need to reuse a file stream to read and write multiple files, you must call clear to clear the stream status before reading another file.

6. Forward Declaration. After the declaration, before the definition, the class is an incomplete type, that is, it is known to be a type, but does not know which members are included. Incomplete types can only be used in a limited manner. Objects of this type cannot be defined. Incomplete types can only be used to define pointers and references pointing to this type, or to declare (rather than define) functions that use this type as the parameter type or return type. Before creating a class object, you must completely define the class. Classes must be defined, not just declared classes. In this way, the compiler will reserve storage space for class objects. Similarly, you must define a class before using a reference or pointer to a member of the model class.

7. Normal references to class objects cannot be returned from the const member function. The const member function can only return * this as a const reference.

8. Reference global variables

int height;void dummy(int height){    ::height = 1;}
The function sets the global variable height to 1 rather than the parameter height to 1.

 

9. You must use the initialization method for any const or reference type member and any member of the class type without the default constructor.

10. It is a good idea to compile the constructor initialization list in the same order as the member declaration. In addition, do not use members to initialize other members.

11. In fact, if other constructors are defined, a default constructor is provided, which is almost always correct. The initial value provided to members in the default constructor should indicate that the object is "null.

12. youyuan is not a member of the class that grants the youyuan relationship, so they are not affected by part of the access control they declare. Generally, it is a good idea to put a friend declaration in groups at the beginning or end of the class definition. Youyuan can be a common non-member function, a member function of another class defined earlier, or the entire class.

13. The static function does not have the this pointer and is not part of any object. It cannot be declared as const or virtual function.

14. static data members must be defined externally in the class definition body

15. The copy constructor can be used to initialize elements in the sequential container, as shown in figure

 

vector
 
   svec(5);
 
The compiler first uses the string default constructor to create a temporary value to initialize the svec, and then uses the copy constructor to copy the temporary value to each element of the svec.

 

16. To prevent replication, the class must explicitly declare that its copy constructor is private. If you want to prohibit the replication of the connected friends and members, you can declare a private copy constructor but do not define it. This leads to an error during the connection.

17. Copying class objects that are not allowed can only be passed as references to or returned from functions. They cannot be used as container elements.

18. The elements in the container are always revoked in reverse order. First, the elements whose subscript is size ()-1 and finally 0 are revoked.

19. Rule 3: If destructor is required, the value assignment operator and the copy constructor are also required.

20. The compositing destructor does not delete the objects pointed to by pointer members.

21. The Destructor has no return value and no form parameter. Because no form parameter can be specified, you cannot overload the destructor. Although a class can define multiple constructors, only one destructor can be provided and applied to all objects of the class.

22. An important difference between a destructor and a copy constructor or a value assignment operator is that even if we write our own destructor, the synthetic destructor still runs. For example, if we write an empty destructor, all the members of the class can also be revoked by the compositing destructor. The merging destructor is executed after the custom destructor.

23. Most C ++ classes use one of the following three methods to manage pointer members:

(1) pointer members adopt regular pointer behavior. Such classes have all pointer defects, but do not require special replication control.

(2) class can implement the so-called "smart pointer" behavior. The Pointer Points to an object that is shared, but the class can prevent the suspension pointer.

(3) Class adopts value-type behavior. The Pointer Points to an object that is unique and managed independently by each class object.

24. Operators that cannot be overloaded

::.*.? :

25. The overload operator must have at least one class type or enumeration type operand. This rule forces the overload operator to not redefine the meaning of the Operator Used for built-in type objects.

26. The operator priority, associativity, or number of operands cannot be changed. In addition to the function call operator (), it is invalid to use the default real parameter when the operator is overloaded.

27. As a class member overload function, the form parameter looks less than the operand 1. As a member function, the operator has an implicit this parameter, which is limited to the first operand. Generally, arithmetic and Relational operators are defined as non-member functions, while value assignment operators are defined as members.

28. It is usually not a good practice to overload commas, get addresses, logical and, logical or other operators. These operators have useful meanings. If we define our own versions, we cannot use these built-in meanings.

29. When the meaning of an overload operator is unknown, it is better to give the operation a name. For rarely used operations, using a naming function is usually better than using an operator. If it is not a common operation, there is no need to use operators for conciseness.

30. To manage container key types and sequence container types, define the ==and <operators on the grounds that many algorithms assume that these operators exist. For example, the sort algorithm uses the <operator, and the find algorithm uses the = Operator.

31. When defining subscript operators in a class, two versions are generally required: one is a non-const member and the other is a const member, and the other returns a const reference.

32. The type conversion function must be a member function. The return type cannot be specified and the parameter table must be empty. Although the conversion function cannot specify the return type, each conversion function must explicitly return a value of the specified type. For example, operator int returns an int value. Conversion functions should not change the converted objects. Therefore, the conversion operator should generally be defined as a const member.

33. The class type cannot be converted to another class type after conversion. If you need to convert multiple class types, the code will fail.

34. A derived class can only access the protected member of its base class through the derived class object. The derived class has no special access permission to the protected member of its base class object.

35. When a virtual function of a derived class calls a base class version, the scope operator must be explicitly used. If the function of the derived class ignores this, the function call is determined at runtime and will be a self-called function, resulting in infinite recursion.

36. During private inheritance, you can use the using Base: XX form in the public part of the derived class, so that the private members of the Base class can be accessed by users.

37. A derived class defined by the class reserved word has private inheritance by default, while a class defined by the struct reserved word has public inheritance by default.

38. Friendship cannot be inherited. The base class's friends do not have special access permissions to the members of the derived class. If the base class is granted a friend relationship, only the base class has special access permissions. The derived class of the base class cannot access the class that grants the friend relationship.

39. If the base class defines static members, there is only one such member in the entire inheritance hierarchy. No matter how many derived classes are derived from the base class, each static member has only one instance.

40. The constructor can only initialize its direct base class because each class defines its own interface. A derived class constructor Cannot initialize a base class member and should not assign values to its base class members.
41. Unlike constructors, the derived class destructor is not responsible for revoking the member of the base class object. The Compiler always explicitly calls the destructor of the base class part of the derived class object. Each destructor is only responsible for identifying its members.

42. Even if no work is required, the root class at the inheritance level should define a virtual destructor.

43. In the replication control, only destructor can be defined as virtual functions, and constructors cannot be defined as virtual functions. The constructor runs before the object is fully constructed. When the constructor runs, the dynamic type of the object is incomplete. Defining the value assignment operator as a virtual function defines an operator = parameter of the base class object in the derived class, which is inconsistent with the value assignment operator in the derived class. Therefore, defining a value assignment operator as a virtual function is confusing and useless.

44. If you call a virtual function in a constructor or destructor, the version of the type defined for the constructor or destructor is run.

45. In the case of inheritance, the scope of the derived class is nested in the scope of the base class.

46. The static type of the object, reference, or pointer determines the behavior that the object can complete. Even when the static type and the dynamic type may be different, just as the reference or pointer of the base class type may occur, the static type still determines what Members can be used.

47. In the scope of the derived class, the derived class member function shields the base class members. The base class members are blocked even if the function prototype is different.

48. If a derived class wants to use all the overloaded versions of its own type, the derived class must either redefine all the overloaded versions or one of them. If you do not want to redefine all, you can provide the using Declaration for the reload member. A using Declaration can only specify one name, but cannot specify a parameter table. Therefore, you can add all the overloaded instances of this function to the scope of the derived class.

49. The virtual function must have the same prototype in the base class and the derived class.

50. C ++ primer P501. A function of the same name in a derived class may block the virtual function of the base class, and thus cannot be called through the derived class object. It can be called through a base class reference or pointer pointing to the derived class object.

51. Because the derived class object will be "Cut off" when assigned to the Base class object, the container and the types related to inheritance cannot be well integrated.

52. The polymorphism on which Object-Oriented Programming depends is called Runtime polymorphism, And the polymorphism on which generic programming depends is called compilation Polymorphism or parametric polymorphism.

53. The name used as the template parameter cannot be reused within the template. This restriction also means that the name of the template parameter can only be used once in the same template parameter.

54. Adding the keyword typename before the template member name can tell the compiler to treat the member as a type.

55. The array parameter can be declared as an array reference. If the parameter is an array reference, the compiler will not convert the array argument to a pointer, but pass the array reference itself. In this case, the array size becomes part of the form parameter and real parameter type. The compiler checks whether the size of the array real parameter matches the size of the form parameter.

56. The explicit template parameters match the corresponding template parameters from left to right.

57. When the compiler sees the template definition, it does not generate code immediately. The compiler generates a template instance of a specific type only when the template is used, for example, the function template or the class template object is called.

58. In the inclusion compilation model, the compiler must see the definitions of all templates used.

59. The real parameter of a non-type template must be a regular expression for compilation.

60. Special and partial special features can have a set of members that are completely different from general class templates.

61. function templates can be overloaded: You can define multiple function templates with the same name but different parameter numbers or types, or define common non-template functions with the same name as the function template.

62. An exception object is created by copying the result of the thrown expression. The result must be of the replicable type.

63. Throwing a pointer is usually a bad idea: throwing a pointer requires that the object to be pointed exists anywhere in the corresponding processing code.

64. During stack expansion, the memory used for local objects is released and the destructor of local objects of the class type are run.

65. When a stack is expanded for an exception, if the Destructor throws another unhandled exception, it will call the standard library terminate function. Generally, the terminate function calls the abort function and forces the entire program to exit abnormally.

66. Unlike the destructor, the constructor often throws an exception when doing something inside the constructor. Therefore, ensure that the constructor is properly revoked.

67. If no matching catch is found, the program calls the database function terminate.

68. The catch capture type must be defined, and the type can't be declared in the forward direction.

69. Generally, if a catch clause is used to handle an exception of the inheritance-related type, it should define its own form parameter as a reference.

70. If catch (...) is used in combination with other catch clauses, it must be the last one. Otherwise, any catch clause following it cannot be matched.

71. The constructor must handle exceptions from the constructor's initialization type. The only method is to compile the constructor as a function test block.

72. Exceptional security means that normal operations can be performed even if an abnormal program occurs, that is, any allocated resources are released as appropriate. You can define a class to encapsulate resource allocation and release to ensure resource release. This technology is often called "resource allocation is initialization", or RAII for short. The Resource Management class should be designed so that the constructor can allocate resources and the Destructor can release resources.

73. autoi_ptr can only be used to manage one object returned from new. It cannot manage dynamically allocated arrays. When auto_ptr is copied or assigned a value, it is abnormal. Therefore, auto_ptr cannot be stored in the standard library container type. The copy and assign values of auto_ptr change the right operand. Therefore, the left and right operands of the assign values must be the left values that can be modified.

74. You should only use get to query the auto_ptr object or use the returned pointer value. You cannot use get as the real parameter for creating other auto_ptr objects.

75. Another difference between the auto_ptr object and the built-in pointer is that an address 9 or another pointer cannot be assigned directly to the auto_ptr object.

76. auto_ptr defects:

77. If no exception is specified in a function declaration, the function can throw any type of exception.

78. during compilation, the compiler cannot and will not attempt to verify the exception description. If a function throws an exception not listed in its exception description, it calls the standard library function unexpected. By default, the unexpected function calls the terminate function, which usually terminates the program.

79. Because the exception description cannot be checked during compilation, the application of the exception description is usually limited. An exception is a useful situation. If a function can ensure that no exception is thrown, it is helpful to both the user and compiler of the function.

80. The exception description of the derived class virtual function must be the same as that of the corresponding base class virtual function, or be more restrictive than the latter. This restriction ensures that when the pointer to the base class type is used to call the virtual function of the derived class, the exception of the derived class does not indicate that a new exception can be thrown.

81. when another pointer is used to initialize a pointer to a function with an exception description, or the latter is assigned to the function address, the exception description of the two pointers does not need to be the same. However, the exception of the source pointer must be at least as strict as that of the target pointer.

82. The namespace can be defined within the global scope or other scopes, but not within the function or class. The namespace scope cannot end with a semicolon.

83. namespaces can be defined in several sections. A namespace consists of the sum of its separated definition parts. A namespace is cumulative.

84. Unnamed namespaces are different from other namespaces. The definition of an unnamed namespace is partial to a specific file and never spans multiple text files. Before the namespace is introduced to C ++, the static statement is used to declare the object name.

85. If the header file defines an unnamed namespace, the names in the namespace define different local entities in each file containing the header file.

86. functions that accept Class-type parameters (or class-type pointers and reference parameters) (including overload operators), and functions (including overload operators) defined in the same namespace as the class itself ), it is visible when a Class Object (or class type reference and pointer) is used as a real parameter.

87. To provide the customization of the template defined in the namespace, you must ensure that the customization is defined in the namespace containing the original template definition.

88. In virtual derivation, the constructor of the underlying derived class initializes the virtual base class. No matter where the virtual base class appears in the inheritance level, the virtual base class is always constructed before the non-virtual base class is constructed.

89. sort uses less by default, which is an incremental sorting.

90. The template function is an instance of the function template.

 

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.