引用与析构,通过引用减少临时变量

引用与析构,通过引用减少临时变量

分类:C/C++语言

通过引用可以大大减少创建临时变量的次数,从而提高程序运行的效率。

本文探讨创建通过引用减少创建临时变量的次数,与临时变量的生命周期。

测试一:不使用引用并且返回的临时变量不用引用保存。

#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();pt.print();return 0;}

运行截图

通过以上输出,可以看出发生了4次析构,说明产生了两个临时变量。

3,4为临时变量。

第一个临时变量为3, 3为传递进来的参数调用了复制构造函数,

生命周期:在函数执行完成后即进行析构。

第二个临时变量为4, 4为返回的参数, 其调用了复制构造函数

生命周期:在完成对原有变量赋值(隐含复制操作)即进行析构。

测试二:不使用引用并且返回的临时变量并使用引用保存。

#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();pt.print();printf("haha\n");return 0;}

运行截图

通过以上输出,可以看出发生了4次析构,说明产生了两个临时变量。

3,4为临时变量。

第一个临时变量为3, 3为传递进来的参数调用了复制构造函数,

生命周期:在函数执行完成后即进行析构。

第二个临时变量为4, 4为返回的参数, 其调用了复制构造函数

生命周期:在完成对原有变量赋值(隐含复制操作)由于又使用了引用对其(临时变量)保存,故该临时变量生存周期同引用生存周期。

在函数最后进行析构,但由于声明变量的顺序与析构顺序相反,析构序号为4,2,1

测试三:对传进来的参数使用引用,返回值不使用引用,返回的临时变量(引用)使用引用保存。

#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();pt.print();printf("haha\n");return 0;}

通过以上输出,可以看出发生了3次析构,说明产生了一个临时变量。

3为临时变量。

第一个临时变量3, 3为返回的参数, 其调用了复制构造函数

生命周期:在完成对原有变量赋值(隐含复制操作)由于又使用了引用对其(临时变量)保存,故该临时变量生存周期同引用生存周期。

在函数最后进行析构,但由于声明变量的顺序与析构顺序相反,析构序号为3,2,1

测试四:对传进来的参数不使用引用,返回值使用引用,返回的临时变量(引用)使用引用保存。

#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();pt.print();printf("haha\n");return 0;}

通过以上输出,可以看出发生了3次析构,说明产生了一个临时变量。

3为临时变量。

第一个临时变量3, 3为传递进去的对象, 其调用了复制构造函数

生命周期:在函数调用的时候使用了复制构造函数,函数执行完成后立即进行析构。

“过去酒逢知已千杯少,现在酒逢千杯知已少”。

引用与析构,通过引用减少临时变量

相关文章:

你感兴趣的文章:

标签云: