您的位置:首页 > 编程语言 > C语言/C++

关于C++函数思考2(C++函数返回引用和返回非引用的区别)

2015-06-07 16:46 609 查看
引用是提高代码效率的一大利器,尤其对于对象来说,当引用作为参数时候不用大面积的复制对象本身所造成的空间与时间的浪费。所以有时候对于参数的返回值我们也希望返回参数的引用。在这里我们回忆一下C语言函数返回局部变量所注意的方面,也可以看我的这篇文章。下来我们对于C++
中函数返回引用或非引用进行探讨!!

1.返回引用

/**********************************************************************
* *   Copyright (c)2015,WK Studios
* *   Filename:  A.h
* *   Compiler: GCC  vc 6.0
* *   Author:WK
* *   Time: 2015 4 5
* **********************************************************************/
#include<iostream>

using std::cout;
using std::cin;
class Test
{
public:
Test(int d=0):m_data(d)
{
cout<<"Create Test Obj :"<<this<<"\n";
}
Test(const Test &t)//提一句 对于拷贝构造函数不能进行对象值传递,因为在值传递的时候会调用拷贝构造,就会一直调用,无限循环
{
cout<<"Copy Test Obj : "<<this<<"\n";
m_data = t.m_data;
}
Test& operator=(const Test &t)
{
cout<<"Assgin:"<<this<<" : "<<&t<<"\n";
if(this != &t)
{
m_data = t.m_data;
}
return *this;
}
~Test()
{
cout<<"Free Test Obj :"<<this<<"\n";
}
int GetData()const
{
return m_data;
}
void print()
{
cout<<m_data<<"\n";
}
private:
int m_data;
};
//方式一
Test& fun(const Test &t)
{
int value = t.GetData();
Test tmp(value);
return tmp;
}

void main()
{
Test t(10);

Test t1;
t1 = fun(t);
cout<<"m_data of t1 is: ";
t1.print();

Test t2 = fun(t);
cout<<"m_data of t2 is: ";
t2.print();
}





2.返回非引用

<span style="color:#333333;">/**********************************************************************
* *   Copyright (c)2015,WK Studios
* *   Filename:  A.h
* *   Compiler: GCC  vc 6.0
* *   Author:WK
* *   Time: 2015 4 5
* **********************************************************************/
#include<iostream>

using std::cout;
using std::cin;
class Test
{
public:
Test(int d=0):m_data(d)
{
cout<<"Create Test Obj :"<<this<<"\n";
}
Test(const Test &t)
{
cout<<"Copy Test Obj : "<<this<<"\n";
m_data = t.m_data;
}
Test& operator=(const Test &t)
{
cout<<"Assgin:"<<this<<" : "<<&t<<"\n";
if(this != &t)
{
m_data = t.m_data;
}
return *this;
}
~Test()
{
cout<<"Free Test Obj :"<<this<<"\n";
}
int GetData()const
{
return m_data;
}
void print()
{
cout<<m_data<<"\n";
}
private:
int m_data;
};
//方式一
Test fun(const Test &t)
{
int value = t.GetData();
Test tmp(value);
return tmp;
}

void main()
{
Test t(10);

Test t1;
t1 = fun(t);
cout<<"m_data of t1 is: ";
t1.print();

Test t2 = fun(t);
cout<<"m_data of t2 is: ";
t2.print();

}
</span>





下来总结一下返回引用和非引用:



再看一个程序:

/**********************************************************************
* Copyright (c)2015,WK Studios
* Filename:
* Compiler: GCC  VC6.0  win32
* Author:WK
* Time: 2015 6 6
************************************************************************/

#include<iostream>
using namespace std;
//#define QUOTE
class S
{
public:
S (int n=0):m_data(n)
{
cout<<"Create the object!"<<this<<"\n";
}
~S()
{
cout<<"Free the object!"<<this<<"\n";
}
#ifdef  QUOTE
S& S::operator =(S&a);
#else
S operator =(S &a);
#endif
S (S &a);
S operator+(S &a);
private:
int m_data;

};
int main()
{
S c(1),b(0);
S a=c=b=(S)99;//从右至左,先是用99生成一个临时对象,在调用两次赋值函数,在调用一次拷贝构造
S d;
d=c=b=(S)99;//vc6.0不支持直接 d=c=b=99但是vs2013支持,本质还是用99构造一个临时对象

S h=a+b+c;//从右向左依次进行相加最后拷贝构造

S j;
j=a+b+c;

S t=c;
return 0;
}
#ifdef QUOTE
S& S::operator =(S&a)
{
if(this!=&a)
{
m_data=a.m_data;
}
cout<<"= quote Operator  "<<this<<endl;
return *this;
}
#else
S S::operator =(S &a)
{
if(this!=&a)
{
m_data=a.m_data;
}
cout<<"= Operator  "<<this<<endl;
return S(*this);
}
#endif

S::S(S &a)
{
cout<<"Copy  "<<this<<endl;
m_data=a.m_data;
}

S S:: operator+(S &a)
{

return S(this->m_data+a.m_data);
}


对于赋值运算符因为返回的是全局的*this所以可以使用引用返回也可以使用对象返回,但是用引用返回不是效率跟高吗?所以大多数选用引用返回

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: