C++ object often reference

we know,A reference to a variable is an alias for the variable.In essence, both variable names and reference names point to the same memory location.

If the parameter is a reference name for a variable,The argument is a variable name.When calling the function to combine virtual and real,It is not to open up another storage space for the formal parameters (often called a copy of the actual parameter), but to pass the address of the actual parameter variable to the formal parameter (reference name) so that the reference name also points to the actual parameter variable.

[Example] Often references to objects.

using namespace std;
class time
  time (int, int, int);
  int hour;
  int minute;
  int sec;
time ::time (int h, int m, int s) //Define the constructor
void fun (time&t)
int main ()
  time t1 (10,13,56);
  fun (t1);
  return 0;

If you do not want to modify the value of the actual parameter t1 in the function, you can declare the reference variable t as const (often reference), the function prototype is

 void fun (const time&t);

Then the value of t cannot be changed in the function, that is, the value of its corresponding actual parameter t1 cannot be changed.

In C++ object-oriented programming,Often use constant pointers and constant references as function parameters.This will ensure data security,So that the data cannot be modified at will,You don't have to make a copy of the arguments when calling the function.

Every time a function is called to create a copy of the arguments,Have to call the copy constructor,Have time.Use constant pointers and constant references as function parameters,Can improve the efficiency of program operation.

C++ object dynamic creation and release

Objects defined using class names are static,While the program is running,The space occupied by the object cannot be released at any time.But sometimes people want to create objects when they are needed,Undo it when you don't need it,Free up the memory space it occupies for other data.This can increase the usage of memory space.

In C++, you can use the new operator to dynamically allocate memory,Use the delete operator to free these memory spaces.This also applies to objects,Objects can be created dynamically using the new operator,Use the delete operator to cancel the object.

If you have defined a box class, you can use the following methods to dynamically create an object:

 new box;

The compilation system opens up some memory space,And store a box object in this memory space, and call the constructor of this class,To initialize the object (if this function has been assigned to the constructor).

But at this time, the user cannot access this object.Because this object has neither an object name,The user does not know its address.Such objects are called unnamed objects,It does exist,But it has no name.

After dynamically allocating memory with the new operator,Will return the value of a pointer to the new object,The starting address of the allocated memory space.Users can get this address,And access this object through this address.You need to define a pointer variable to an object of this class to store the address.Such as

 box * pt;//Define a pointer variable pt to a box object
  pt=new box;//The starting address of the newly created object is stored in pt

In the program, you can access this newly created object through pt.Such as

 cout<pt->height;//Output the height member of the object
  cout<pt->volume ();//Call the volume function of the object, calculate and output the volume

C++ also allows newly created objects to be initialized when new is executed.Such as

 box * pt=new box (12,15,18);

This way of writing is to combine the above two statements (defining pointer variables and creating new objects with new) into one statement,And specify the initial value.This is more refined.

The height, width, and length in the new object get the initial values ​​of 12, 15, 18, respectively. The calling object can be passed either the object name,You can also pass a pointer.

Dynamic objects created with new generally do not use object names.Is accessed through a pointer,It is mainly applied to dynamic data structures,Such as linked lists. Access the nodes in the linked list,Does not need to pass the object name,Instead, the address of the next node is stored in the previous node.To find the next node from the previous node,The relationships that make up the link.

When performing new operations, if the amount of memory is insufficient,Unable to open up the required memory space,Most current C++ compilation systems make new return a 0 pointer value. As long as the return value is 0, you can determine whether the memory allocation was successful.

Ansi c++ standard proposed,When the execution of new fails,Just "throw" an "exception", and the user can perform related processing according to the exception.But the C++ standard still allows 0 pointer values ​​to be returned in the event of a new fault. Currently, different compilation systems deal with new faults differently.

When you no longer need to use objects created by new,Can be released with the delete operator.Such as

 delete pt;//Free up memory space pointed to by pt

This undoes the object pointed to by pt.The program can no longer use the object afterwards.

If you use a pointer variable pt to point to different dynamic objects,Note that the pointer variable is currently pointing,So as not to delete the wrong object.When executing the delete operator,Before freeing memory space,Call the destructor automatically,Complete the aftermath cleanup.

  • Previous Analysis of Basic MVC Examples for Java Web Implementation
  • Next Ajax asynchronously request Json data (detailed graphic)