When referencing C ++, it is like a constant pointer that can be automatically referenced by the compiler in reverse order. The idea referenced in C ++ comes from the ALGOL language. References are the basis for supporting the syntax of the C ++ operator overload, and facilitate the transfer control of function parameters. A copy constructor is a special constructor. It must be referenced to generate new objects from existing objects of the same type. The compiler uses the copy constructor to pass and return objects by passing values.
The difference between C and C ++ pointers is that C ++ is a language with higher type requirements. C is not allowed to assign a pointer of A type to another object, but it can be implemented through void. C ++ does not allow this.
Reference (&) is like a constant pointer that can be reversed by the compiler. The parameters or return values of user functions. But it can also be used independently. When creating a reference, the reference must be initialized to execute an existing object. The reference must be associated with the storage unit. when accessing the reference, it is actually accessing that storage unit. The simplest way to consider a reference is to treat it as a strange pointer. The advantage of this pointer is that you don't have to consider whether it is initialized. the compiler will force it to initialize, and you don't have to know how to reverse reference it. There are certain rules when using the reference: 1) when the reference is created, it must be initialized (the pointer can be initialized at any time ). 2) Once a reference is initialized to point to an object, it cannot be changed to a reference to another object (the pointer can point to another object at any time ). 3) null reference is not allowed. Make sure that the reference is associated with a valid storage unit.
The most common reference of a function is the function parameter and return value. When used as a function, any changes to the reference in the function will change the parameters outside the function. This can also be done through pointers, but the syntax is not clear. If a reference is returned from a function, it must be treated like a pointer returned from the function. It is particularly important to use constant references in function parameters. A function may accept a temporary object. This temporary object is explicitly created by the return value or function of a function. The temporary object remains unchanged. Therefore, if constant reference is not used, parameters will not be accepted by the compiler. In C, If You Want To reference a pointer and change the pointer itself rather than the content it points to, the function declaration may be: void F (INT **), the pointer address must be obtained during transmission. In C ++, the function parameter becomes a pointer reference, and the pointer address is not required.
One way to improve efficiency is to call constructor and destructor in the value passing method. If you do not want to change the parameter, you can pass it through constant reference, it only needs to push the address to the stack. There is only one situation that does not apply to the transfer address method. That is, when the pass value is the only safe way, otherwise it will destroy the object (instead of modifying the external object ).
A copy constructor is usually called X (& X) (X referenced by X ). During function calling, this constructor is the root of the transfer and return types by passing values. In C and C ++, parameters are pushed from right to left into the stack, and then the function is called. The code is used to clear parameters in the stack. When parameters are passed by passing values, the compiler simply copies the parameters to the pressure stack and generates a correct copy for the pressure stack parameters. The compiler places the returned value in the Register to return it. Copying this bit is equivalent to copying an object. When the compiler generates code for a function call, it first pushes all the parameters to the stack, then calls the function, generates code within the function, and moves down the stack pointer to provide storage units for the function's local variables. In assembly language call, the CPU puts the address of the function call instruction in the Code on the stack. The Assembly Language return can be used to return the address to the call point. When the Register does not store the returned value in a large enough place, it will direct the returned value to a function parameter and press the stack, so that the function can directly copy the returned value information to the destination.
The method used to transmit and return large simple structures is to copy bitwise from one place to another, which is feasible for C. However, in C ++, objects are much richer than a group of bits. When an object is passed by passing values, a new object is created. Objects in the function body are transmitted by objects that exist in the original function in vitro. The same is true for returning a function. When the compiler creates a new object from an existing object, it can define its own function to do something. Because it is creating a new object, this function should be a constructor, the single parameter passed to this function is the source object of the created object, but this object cannot be passed into the constructor, it is meaningless to try to define a function for processing the pass-through method to pass a pointer by syntactic structure. Therefore, you can use the reference of the source object. This function is called a copy constructor. If a copy structure is not created for a more complex type, the C ++ compiler automatically creates a copy constructor. You must copy the constructor only when you want to pass class objects by passing values. There is a simple way to prevent passing by passing values: to create a private copy constructor. You don't even need to define it unless the member function or the friend function needs to pass the value in the pass-through mode.
Generally, the reference syntax is better than the pointer syntax. when passing a variable parameter, it may be safer to use the pointer from the perspective of code maintenance. Unless you want to modify the external object through the address, you will use the const reference to transfer the address.
A pointer is a variable pointing to a memory address. It can be a data address or a function address. So you can change the address pointing to the function at runtime. In addition to the class content selected by the member pointers of C ++, the member pointers of C ++ follow the same principle. However, because the pointer requires an address, but the class does not have an address, selecting a class member means the offset in the class. The actual address can be obtained only when the offset is combined with the start address of a specific object. The syntax of the member function requires that the member pointer be referenced in reverse direction when an object is selected. In order to obtain the content pointed to by the class member pointer, the * symbol must be used for reverse reference. However, because it is only the offset in the object, you must specify the object. Therefore, the * sign must be used with the reverse referenced object. The new syntax for a pointer to an object is-> *, and for an object or reference is .*. For member pointers generated by member functions, such as the format: int (* FP) (float ). (* FP) parentheses force the compiler to correctly determine the definition. If no value exists, the expression returns an int * value function.