Regular reference dynamic creation and release of objects in C++

  • 2020-05-05 11:29:40
  • OfStack

C++ object's regular reference
We know that a reference to a variable is an alias for the variable. Essentially, both the variable name and the reference name point to the same segment of memory.

If the parameter is the reference name of the variable and the argument is the name of the variable, then instead of creating another storage space for the parameter (often called a copy of the argument) when the function is called to combine the virtual and real arguments, the address of the parameter is passed to the parameter (the reference name), so that the reference name also points to the parameter.

A regular reference to an object.


#include <iostream>
using namespace std;
class Time
{
  public:
  Time(int,int,int);
  int hour;
  int minute;
  int sec;
};
Time::Time(int h,int m,int s) // Define the constructor 
{
  hour=h;
  minute=m;
  sec=s;
}
void fun(Time &t)
{
  t.hour=18;
}
int main( )
{
  Time t1(10,13,56);
  fun(t1);
  cout<<t1.hour<<endl;
  return 0;
}

If you do not want to change the value of the argument t1 in a function, you can declare the reference variable t as const(often referred to) and prototype the function as


  void fun(const Time &t);


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

In C++ object-oriented programming, constant Pointers and constant references are often used as function parameters. This ensures that the data is secure, that it cannot be modified at will, and that you do not have to create a copy of the arguments when you call the function.

Each time a function is called to create a copy of an argument, the copy constructor is called and there is a time cost. Using constant pointer and constant reference as function parameters can improve the efficiency of the program.


C++ object dynamic creation and release
The objects defined by the class name are static, and the space occupied by the objects cannot be released at any time during the program running. But sometimes people want to create objects when they are needed, and then undo them when they are not needed, freeing up memory for other data. This improves memory space utilization.

In C++, you can use the new operator to dynamically allocate memory and the delete operator to free up that memory space. This also applies to objects, which can be created dynamically with the new operator and revoked with the delete operator.

If you have already defined an Box class, you can dynamically create an object using the following method:


  new Box;


The compilation system allocates a memory space in which to store an Box class object, and calls the constructor of that class to initialize the object (if the constructor has been assigned this function).

But at this point the user cannot access the object, because the object has no object name and the user does not know its address. This object is called an unnamed object, and it does exist, but it has no name.

When memory is allocated dynamically with the new operator, a value pointing to a new object is returned, the starting address of the allocated memory space. The user can obtain this address and access the object from this address. You need to define a pointer to an object of this class to hold the address. Such as


  Box *pt; // Define a point Box Pointer to a class object pt
  pt=new Box; // in pt Contains the starting address of the new object 


The newly created object can be accessed from pt in the program. Such as


  cout<<pt->height; // The output of this object height Members of the 
  cout<<pt->volume( ); // To invoke the object volume Function, calculate and output the volume 

C++ also allows initialization of newly created objects when new is executed. Such as


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


This is written by combining the two statements above (defining pointer variables and creating new objects with new) into one statement and specifying the initial value. It's more refined.

height, width, and length in the new object get an initial value of 12,15, and 18, respectively. Objects can be called either by object name or by pointer.

Dynamic objects created with new are generally not object names, but are accessed through Pointers, which are mainly applied to dynamic data structures, such as linked lists. To access the nodes in the linked list, it is not necessary to store the address of the next node in the previous node through the object name, so as to find the next node from the previous node and form a link relationship.

When performing an new operation, most current C++ compilation systems have new return a 0 pointer if there is not enough memory to carve out the required memory space. As long as the return value is 0, you can determine whether the memory allocation was successful.

The ANSI C++ standard proposes that when the execution of new fails, an "exception" is "thrown", which can be handled by the user. However, the C++ standard still allows a 0 pointer value to be returned in the event of an new failure. Currently, different compilation systems handle the new fault differently.

When objects created by new are no longer needed, they can be freed with the delete operator. Such as


  delete pt; // The release of pt Point to the memory space 


This cancels the object that pt points to. The program can no longer use the object after that.

If you use a pointer variable pt to point to different dynamic objects successively, you should pay attention to the current point of the pointer variable to avoid deleting the wrong object. When the delete operator is executed, the destructor is automatically called to complete the cleanup before the memory space is freed.


Related articles: