1. default constructor, value assignment function, default copy constructor
Classa A; default constructor
A = B; Value assignment
Classa (const classa &) copy constructor
**************************************** *****
When the function passes the value, it will call the copy constructor to construct a temporary object, and parse the temporary object upon return.
**************************************** *****
The default copy constructor is only a simple copy member. Therefore, if it is a reference member, you must be careful with it. The default function may not be suitable.
Saving the value assignment ratio by using the initialization list for Class Members
Note that the structure order of members is defined in the class order rather than the initialization list order.
The default value assignment function is almost the same as the default copy constructor, but the following differences must be noted:
**************************************** *****
The default value assignment function can assign values to itself. You need to modify the original value. The value assignment function can return values.
**************************************** *****
Bytes -------------------------------------------------------------------------------------------------
2. interfaces and Implementations
Providing external access methods for member variables, rather than exposing members, saves the trouble of future modification and implementation.
The constructor of a parameter has implicit conversion.
Type conversion operators should not be defined. Otherwise, they are prone to ambiguity.
=, [], (), And-> must be implemented as member functions, rather than global operators.
Note: The Global operator overload can be implicitly converted to the first parameter.
The default pointer and reference parameter have a small trap.
Void F (const char * = ""); // * And = must be written separately
Void F (const char * = "");
Application of constant const
The temporary variable passed to the function as a non-constant pointer is invalid.
Constant member functions are defined as follows:
Int langth () const {};
Note: In the constant member function definition body, all the member variables of the object are constants, including the this pointer.
The initialization sequence of static data in different files is not clearly defined.
Use handle hiding
Declare only one pointer handle
Class classa;
Class classb
{
Classa * m_pobja;
Define public interfaces
}
// Implement classa for private files
Class classa
{
}
In this way, if classa is changed, the user only needs to link and does not need to re-compile.
Bytes -------------------------------------------------------------------------------------------------
3. Notes on inheritance
Inheritance: is-a relationship
Combination: has-a relationship
Note: Any behavior that restricts operations in a derived class usually leads to mistakes in the design of the inherited architecture.
Public inheritance
The base class is public in the derived class, while the protected class is still private.
You can implicitly convert the pointer of a derived class to a base class pointer.
Private inheritance (can be replaced by combinations)
The base class public and protection are both private, and the base class private cannot be accessed in the derived class
You cannot implicitly convert the pointer of a derived class to a base class pointer.
Protection inheritance
Changes in the public and protection of the base class to protection,
You can implicitly convert the pointer of a derived class to a base class pointer.
The inheritance of virtual functions must ensure the consistency of abstraction.
The Destructor cannot be defined as pure virtual functions.
What is not inherited during inheritance
Constructor
Destructor
Value assignment operator
Hidden functions (if the same name of the base class is defined in the derived class but the parameters are inconsistent, the base class functions are hidden)
Calling virtual functions in the constructor and constructor calls virtual functions of the base class.
Bytes -------------------------------------------------------------------------------------------------
4. Multi-Inheritance
Use the virtual base class to solve the multi-copy problem of the base class
Ambiguity of functions with the same name can be eliminated using the definition in the final derived class.
The construction of the virtual base class occurs in the constructors at the outermost layer. Therefore, it is necessary to explicitly initialize the virtual base class in the constructors of all derived classes.
Bytes -------------------------------------------------------------------------------------------------
5. Template
For a specific name, only one template class is defined in the program and cannot be overloaded.
The keyword template and type parameters must appear in each definition.
For example:
Template <class T>
Class Stack {
Stack ();
}
Template <class T>
Stack <t>: Stack (){
}
Instantiation repeats until all instances are instantiated.
When template parameters use template classes, note the space between two>
For example:
List <set <int> ABC
Template Functions
Template <class T>
Void swap (T & A, T & B ){
}
Call can be called directly, and find the correct function through the function overload mechanism, without explicitly specifying
Int x = 5, y = 2;
Swap (x, y );
Note that there is no implicit conversion
The template class can provide a special version by redefining classes by type
For example:
Class list <char> {
// Redefinition
}
Template functions can also be customized or overloaded.
Bytes -------------------------------------------------------------------------------------------------
6. Reuse rules
When using referenced parameters, pay attention to the generation of a null pointer.
Garbage Collection Mechanism
Arena Solution
You can use parentheses to add new parameters.
For example
Void * operator new (size_t bytes, int nparam ){
}
Thing * P = new (nparam) thing;
Exit (), exec (), abort () will cause the static object to be destroyed, but will not destroy the automatic object.
It helps to detect bugs by filling in meaningless values in destructor
The new and delete types specific to the class can be inherited, and the new parameter automatically changes to the size of the derived class.
Bytes -------------------------------------------------------------------------------------------------
7. Exceptions
Throw an exception. The parameter can be of any type.
When the exception processor needs to continue to throw, it can throw directly without Parameters
Automatic variables can be destroyed for exceptions, but will not be cleared for heap objects.