Destructors for further discussion of classes and objects C + +

Source: Internet
Author: User

Destructors are also a special member function. It works in the opposite direction of the constructor function. Its name is preceded by a "~" symbol in front of the class name . In C + + "~" is a bitwise inverse operator . The parse function is automatically executed when the object's life ends. Destructors are performed in the following situations:

1. If an object is defined in a function, when the function is called at the end, the object should be freed and the destructor automatically executed before the object is released.

2.static a local object is not disposed at the end of a function call, so the destructor is not called, and the destructor for the Staitic local object is called only when the main function ends or calls the Exit function.

3. If a global object is defined, the destructor for the global object is called when the program's process leaves its scope, such as the main function end or Exit function.

4. If an object is created dynamically with the new operator, the destructor for that object is called first when the object is disposed with the delete operator.

The function of a destructor is not to delete an object, but to do some cleanup before undoing the memory occupied by the object, so that the memory can be allocated to the new object for use by the program. The program designer has designed the destructor in advance to complete the required functions, so that the program performs the destructor automatically as long as the object's life is over.

Destructors do not return any values, no function types, and no function arguments. Because there is no function argument, it cannot be overloaded. And there can be only one destructor in a class.

destructors can also be used to perform any actions that the user wants to perform after the object is last used . For example, output information about. This means that the user refers to the designer of the class. Destructors can accomplish any action that the designer specifies.

If you want the destructor to do any work, you must specify it in the destructor of the definition .

Order of calling constructors and destructors

In general, the order in which the destructors are called is exactly the same as calling the constructor, the first called constructor, whose corresponding destructor was finally called, and the last constructor that was called, whose corresponding destructor was first called. The first structure of the post-destructor , and the first reconstruction of the post-construction . (not always in accordance with this principle.) differ in the concept of scope and storage class). objects can be defined in different scopes, but there is a difference.

Here's a summary of when constructors and destructors are called:

1. Objects defined at the global scope (that is, objects defined outside of all functions), its constructors are called before all functions in the file (including the main function) are executed. However, if there are multiple files in a program, and the different files define global objects, the order in which the constructors of those objects are executed is undefined . The destructor is called when the main function finishes executing or calls the Exit function (at which point the program terminates).

2. If you define a local automatic object, such as defining an object in a function, the constructor is called when the object is created. If the function is called more than once, the constructor is called every time the object is created. The destructor is called before the function call ends and the object is disposed.

3. If the static static local object is defined in a function, the constructor is called only once when the program first calls the function to establish the object, the image is not disposed at the end of the call, and therefore no destructor is called, and the destructor is called only at the end of the main function or when the Exit function is called to end the program.

Object array

The constructor is also called when an array is created, and if there are 50 elements, it is necessary to call 50 constructors. You can provide an argument when you need to define an array to implement initialization. If the constructor has only one parameter, you can provide the arguments directly in the curly braces following the equals sign when you define the array. Such as

Student stud[3]={60,70,80};

If the constructor has more than one parameter, you cannot provide the method of all arguments directly when you define the array, because an array has multiple elements and provides multiple arguments for each element, so it is easy to make the correspondence between the arguments and formal parameters unclear and ambiguous if the constructor has default arguments.

So, if the constructor has more than one parameter, how should the initialization be implemented when an array of objects is defined? Answer: Write the constructor in curly brackets and specify the argument, if the constructor has 3 parameters, representing the school number, age, and score respectively. You can define an array like this:

Student stud[3]={//Defining an array of objects

Student (1001,19,87); Call the constructor of the first element, giving him three arguments

Student (1002,19,78); Call the constructor of the second element, giving him 3 arguments

Student (1003,18,79); //  .......................

};

Example:

#include <iostream>
using namespace Std;
Class Box
{
Public
Box (int h,int w,int len): Height (h), Width (w), Length (len) {}
int volume ();
Private
int height;
int width;
int length;
};

int Box::volume ()
{
return (height*width*length);
}

int main ()
{
Box A[3]={box (10,12,15), Box (15,18,20), Box (16,20,26)};
cout<< "Volume of A[0" is "<<a[0].volume () <<endl;
cout<< "Volume of A[1" is "<<a[1].volume () <<endl;
cout<< "Volume of A[2" is "<<a[2].volume () <<endl;
return 0;
}

Object pointers

A pointer to an object where the starting address of the object space is a pointer to the object.

Class Name * object pointer name;

A pointer to an object member with a pointer to the pointer variable that holds the object's initial address. The member in the object also has an address, and a pointer variable that holds the address of the object member is a pointer variable that points to the object member.

The method that defines a pointer variable that points to an object data member is the same as a pointer variable method that defines an ordinary variable, for example: int *p;

Defines the general form that points to object data members and pointer variables:

Data type name * pointer variable;

If the data member of the time class is hour as a common integer data, the object data member hour can be accessed outside the class through pointer variables that point to the object data member.

p1=&t1.hour;

cout<<*p1<<endl;

Pointers to object member functions, methods for defining pointer variables to object member functions, and pointer variable methods that define pointers to normal functions are different. The following is the definition of a pointer variable for a normal function: data type name (* pointer variable name) (parameter list);

such as: void (*p) (); P is a pointer variable that points to void type function

You can make it point to a function and call it through a pointer variable.

P=fun; Assigning the entry address of the fun function to the pointer variable p,p points to the function fun

(*p)                                 (); Call the Fun function

The most fundamental difference between a member function and a normal function is that it is a member of a class. The compilation system requires that in the assignment statement above, the type of the pointer variable must match the type of the function on the right of the assignment number, requiring the following 3 aspects to match : 1. The type and number of parameters of the function parameter ; 2. type of function return value ; 3. the class to which it belongs .

Pointer variables that point to member functions should be defined as follows:

void (time::* p2) (); Defines p2 as a pointer variable that points to a public member function in the time class.

Note: The parentheses on either side of (time::* P2) cannot be omitted because () has a higher precedence than *.

The general form of a pointer variable that defines a public member function is:

Data type name (class name:: * pointer variable name) (parameter list);

You can point it to a common member function by assigning the entry address of a public member function to a pointer variable that points to a public member function. For example:

p2=&time::get_time;

Make pointer variables the general form of a common member function:

Pointer variable name =& class Name:: member function name;

Example:

#include <iostream>
using namespace Std;
Class time
{
Public
Time (Int,int,int);
int hour;
int minute;
int sec;
void Get_time ();
};

Time::time (int h,int m,int s)
{
Hour=h;
Minute=m;
Sec=s;
}

void Time::get_time ()
{
cout<}

int main ()
{
Time T1 (15,12,55);
int *p1=&t1.hour;
cout<<*p1<<endl;
T1.get_time ();
Time *p2=&t1;
P2->get_time ();
void (time::* p3) ();
p3=&time::get_time;
(T1.*P3) ();
}

Destructors for further discussion of classes and objects C + +

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.