References and destructors, reducing temporary variables by reference

Source: Internet
Author: User

References can greatly reduce the number of times a temporary variable is created, thus increasing the efficiency of the program's operation.

This article explores the creation of a temporary variable by reference to reduce the number of times the lifetime of the temporary variable.


Test one: Do not use references and return temporary variables without reference to save.


#include <cstdio> #include <iostream>using namespace std;class point{private:static int count;int x;public: Point () {printf ("constructor called\n"); x = ++point::count;} Point (const point &b) {printf ("copy constructor called\n"); x  = ++point::count;} ~point () {printf ("xx%d\n", x);} Point XX (point x) {//this->x = 0;return x;} void print () const{printf ("%d\n", x);}}; int point::count = 0;int Main () {Point pt; Point Pk;//point &pz = pt. XX (PK);//pz.print ();p t.print (); return 0;}


Run


With the above output, it can be seen that 4 destructors have occurred, indicating that two temporary variables have been generated.

3,4 is a temporary variable.

The first temporary variable is 3, and 3 calls the copy constructor for the passed in parameter.

Life cycle: Destructors are performed after the function is completed.

The second temporary variable is 4, and 4 is the returned parameter, which calls the copy constructor

Life cycle: The completion of assignment of the original variable (implicit copy operation) is the destructor.




Test two: do not use references and return temporary variables and save using references.

#include <cstdio> #include <iostream>using namespace std;class point{private:static int count;int x;public: Point () {printf ("constructor called\n"); x = ++point::count;} Point (const point &b) {printf ("copy constructor called\n"); x = ++point::count;} ~point () {printf ("xx%d\n", x);} Point XX (point x) {//this->x = 0;return x;} void print () const{printf ("%d\n", x);}}; int point::count = 0;int Main () {Point pt; Point PK; Point &pz = Pt. XX (PK);//pz.print ();p t.print ();p rintf ("haha\n"); return 0;}



Run cutFigure


With the above output, it can be seen that 4 destructors have occurred, indicating that two temporary variables have been generated.

3,4 is a temporary variable.

The first temporary variable is 3, and 3 calls the copy constructor for the passed in parameter.

Life cycle: Destructors are performed after the function is completed.

The second temporary variable is 4, and 4 is the returned parameter, which calls the copy constructor

Life cycle: In the completion of the original variable assignment (implicit copy operation) due to the use of a reference to its (temporary variable) save, so the temporary variable life cycle of the same reference life cycle.

At the end of the function, the destructor sequence is 4,2,1 because the order of the declared variables is the opposite of the destructor order.



Test Three: Use a reference to the passed in parameter, the return value does not use the reference, the returned temporary variable (reference) is saved with a reference.

#include <cstdio> #include <iostream>using namespace std;class point{private:static int count;int x;public: Point () {printf ("constructor called\n"); x = ++point::count;} Point (const point &b) {printf ("copy constructor called\n"); x = ++point::count;} ~point () {printf ("xx%d\n", x);} Point XX (Point &x) {//this->x = 0;return x;} void print () const{printf ("%d\n", x);}}; int point::count = 0;int Main () {Point pt; Point PK; Point &pz = Pt. XX (PK);//pz.print ();p t.print ();p rintf ("haha\n"); return 0;}



With the above output, it can be seen that 3 destructors have occurred, indicating that a temporary variable has been generated.

3 is a temporary variable.

The first temporary variable 3, 3 is the returned parameter, and it calls the copy constructor

Life cycle: In the completion of the original variable assignment (implicit copy operation) due to the use of a reference to its (temporary variable) save, so the temporary variable life cycle of the same reference life cycle.

At the end of the function, the destructor sequence is 3,2,1 because the order of the declared variables is the opposite of the destructor order.



Test Four: The arguments passed in are not referenced, the return value uses a reference, the returned temporary variable (reference) is saved with a reference.

#include <cstdio> #include <iostream>using namespace std;class point{private:static int count;int x;public: Point () {printf ("constructor called\n"); x = ++point::count;} Point (const point &b) {printf ("copy constructor called\n"); x = ++point::count;} ~point () {printf ("xx%d\n", x);} point& XX (point x) {//this->x = 0;return x;} void print () const{printf ("%d\n", x);}}; int point::count = 0;int Main () {Point pt; Point PK; Point &pz = Pt. XX (PK);//pz.print ();p t.print ();p rintf ("haha\n"); return 0;}


With the above output, it can be seen that 3 destructors have occurred, indicating that a temporary variable has been generated.

3 is a temporary variable.

The first temporary variable 3, 3 is the passed in object, and it calls the copy constructor

Life cycle: The copy constructor is used when the function is called, and the function executes immediately after the execution is done.




Test Five: Use a reference to the passed in parameters, return the value using a reference,the returned temporary variable (reference) is saved with a reference.

#include <cstdio> #include <iostream>using namespace std;class point{private:static int count;int x;public: Point () {printf ("constructor called\n"); x = ++point::count;} Point (const point &b) {printf ("copy constructor called\n"); x = ++point::count;} ~point () {printf ("xx%d\n", x);} point& XX (Point &x) {//this->x = 0;return x;} void print () const{printf ("%d\n", x);}}; int point::count = 0;int Main () {Point pt; Point PK; Point &pz = Pt. XX (PK);//pz.print ();p t.print ();p rintf ("haha\n"); return 0;}


With the above output, we can see that 2 destructors have occurred, indicating that no temporary variables have been generated.



Test Six: The arguments passed in are not referenced, the return value uses a reference, the returned temporary variable (reference) is saved with a reference.

#include <cstdio> #include <iostream>using namespace std;class point{private:static int count;int x;public: Point () {printf ("constructor called\n"); x = ++point::count;} Point (const point &b) {printf ("copy constructor called\n"); x = ++point::count;} ~point () {printf ("xx%d\n", x);} point& XX (point x) {//this->x = 0;return x;} void print () const{printf ("%d\n", x);}}; int point::count = 0;int Main () {Point pt; Point PK; Point &pz = Pt. XX (PK);p z.print ();p t.print ();p rintf ("haha\n"); return 0;}


With the above output, it can be seen that 3 destructors have occurred, indicating that a temporary variable has been generated.

3 is a temporary variable.

The first temporary variable 3, 3 is the passed in object, and it calls the copy constructor

Life cycle: The copy constructor is used when the function is called, and the function executes immediately after the execution is done. And I used a reference to save the temporary variable that was returned, but in the completion

After assigning a value

Point &pz = Pt. XX (PK);

That is, after this statement, the temporary variable is destroyed, and then the print function called after the release of the memory area, you need to pay great attention to



Test Seven: Use a reference to the passed in parameter, the return value does not use the reference, the returned temporary variable (reference) is saved with a reference.

#include <cstdio> #include <iostream>using namespace std;class point{private:static int count;int x;public: Point () {printf ("constructor called\n"); x = ++point::count;} Point (const point &b) {printf ("copy constructor called\n"); x = ++point::count;} ~point () {printf ("xx%d\n", x);} Point XX (Point &x) {//this->x = 0;return x;} void print () const{printf ("%d\n", x);}}; int point::count = 0;int Main () {Point pt; Point PK; Point &pz = Pt. XX (PK);p z.print ();p t.print ();p rintf ("haha\n"); return 0;}


By testing 1-6, it is not difficult to find that the life cycle of the temporary object is the life cycle of the reference because the object returned is an assignment, so in PRITNF ("haha\n");

Release when main ends.




Through the above tests, we can see

1. If the argument passed by the function is an object, you need to return the object.

References should be used when the function passes the arguments and when the function returns, which reduces the generation of the temporary variable two times. (Test one ~ Test five)


2. If you call the copy constructor on a parameter, return the use reference, and after the assignment is complete, the memory space of the temporary variable generated by the copy constructor is recycled (see test VI)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

References and destructors, reducing temporary variables by reference

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.