The common reference, dynamic establishment and release knowledge of objects in C + + explain _c language

Source: Internet
Author: User

Common references to C + + objects
We know that a reference to a variable is the alias of the variable. In essence, variable names and reference names point to the same segment of the deposit.

If the parameter is the reference name of the variable, an argument is a variable name, and when the function is invoked to create an actual combination, it is not a separate storage space for the formal parameter (often called a copy of the argument), but rather the address of the argument variable to the formal parameter (the reference name), so the reference name also points to the argument variable.

A constant reference to the [Example] 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 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 modify the value of the argument T1 in the function, you can declare the reference variable t to be a const (constant reference), and the function prototype is

  void Fun (const time &t);


The value of T can not be changed in the function, that is, the value of the corresponding argument T1 cannot be changed.

In the object-oriented programming of C + +, the function parameters are often referred to as common pointers and constant references. This will not only ensure data security, so that the data can not be arbitrarily modified, when the function is called without the need to establish a copy of the argument.

A copy constructor is invoked every time the function establishes a copy of the argument, and there is a time overhead. Using constant pointer and constant reference as function parameters can improve the efficiency of program operation.


Dynamic establishment and release of C + + objects
objects defined with the class name are static, and the space occupied by the object cannot be released at any time during the program's operation. But sometimes people want to set up an object when they need it, undo it when it's not needed, and free up the memory space it occupies for other data to use. This can increase the utilization of memory space.

In C + +, you can use the new operator to allocate memory dynamically, freeing the memory space with the delete operator. This also applies to objects, which can be created dynamically with the new operator, and the delete operator to undo the object.

If you have defined a box class, you can create an object dynamically in the following ways:

  New Box;


The compilation system creates a memory space and holds a box object in this memory space, calling the constructor of the class to initialize the object (if the constructor has been given this function).

However, the user cannot access the object at this time because the object has no object name and the user does not know its address. This object is called a nameless object, it does exist, but it does not have a name.

When memory is allocated dynamically with the new operator, it returns a value that points to a pointer to the object, the starting address of the allocated memory space. The user can obtain this address and access the object through this address. You need to define a pointer variable that points to an object of this class to hold the address. Such as

  Box *pt; Defines a pointer variable that points to the box object PT
  pt=new box;//The starting address of the new object is stored in PT


In the program, you can access this new object through Pt. Such as

  cout<<pt->height; Output the height member of the object
  Cout<<pt->volume ();//Call the object's volume function to 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 a combination of the above two statements (defining the pointer variable and creating a new object with a) into a single statement and specifying the initial value. This is more refined.

The height,width and length of the new object get the initial value 12,15,18 respectively. The calling object can be either through the object name or through the pointer.

The dynamic objects that are built with new are generally used without object names and are accessed by pointers, which are mainly applied to dynamic data structures, such as linked lists. Access to the node in the linked list, not through the object name, but in the last node to store the address of the next node, so that the last node to find the next node, forming a link relationship.

When you perform the new operation, if there is not enough memory to open up the required memory space, most C + + compilation Systems now bring new a 0 pointer value. You can determine whether the allocated memory is successful as long as the return value is detected as 0.

The ANSI C + + standard proposes to "throw" an "exception" when a new failure occurs, and the user can handle it according to the exception. However, the C + + standard still allows 0 pointer values to be returned when a new failure occurs. At present, different compiler systems have different methods to deal with new faults.

When you no longer need to use an object created by new, you can use the delete operator to release it. Such as

  Delete pt; Release PT point to memory space


This revokes the object the PT points to. This object can no longer be used by the program thereafter.

If you use a pointer variable pt successively points to different dynamic objects, you should pay attention to the current direction of the pointer variable, so as not to delete the wrong object. When the delete operator is executed, the destructor is automatically invoked before the memory space is freed to complete the clean-up work.

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.