函数返回引用问题!

majiayefengkuanghaha 2018-03-05 08:32:01
下面代码合法吗?
int& get(int *array,int index)
{
return array[index];
}

大神能多举例子吗?

我怎么看有些书函数返回局部对象的引用合法?
...全文
958 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
yinxueju_12 2018-03-16
  • 打赏
  • 举报
回复
没有任何问题啊,建议你反汇编一下。实际上int & 只是个帽子,具体操作还是地址啊,且在运行栈一般也没开辟内存。
Acuity. 2018-03-12
  • 打赏
  • 举报
回复
一般不建议返回引用,稍微不注意可能导致内存泄露。
suuare 2018-03-11
  • 打赏
  • 举报
回复
同意楼上,只要返回的对象本身没有销毁,那么就可以返回这个对象的引用,除了上面的里例子,这样也行:

int foo()
{
     static int n ;
     //....
     return n;
}
//或者
int g_n;
int bar()
{
     //.....
      return g_n;
}
cyan_sxm 2018-03-06
  • 打赏
  • 举报
回复
引用是合理的,传入的array是形参
xiaoxiangqing 2018-03-06
  • 打赏
  • 举报
回复
你这个例子是ok的
  • 打赏
  • 举报
回复
搞 #include <iostream> #include <vector> using namespace std; const int& findmax(const vector& a); int main() { vector<int> a = {1,2,3}; cout << findmax(a) << endl; } const int& findmax(const vector<int>& a) { int maxindex = 0; for(int i =1; i < a.size(); ++i) { if(a[maxindex])<a[i]) { maxindex = i; } else { i = maxindex; } } return a[maxindex]; }
考拉一枚 2018-03-06
  • 打赏
  • 举报
回复
看合不合法就是看函数结束调用后引用所指向的对象是否存在,如果该对象是局部变量不存在了 那你当然不能用引用去操作它,而你这个函数里传给形参的值是int *类型变量值的拷贝,array[index]的生命周期并不依赖于该函数,所以引用合法 ,只需注意越界问题
真相重于对错 2018-03-05
  • 打赏
  • 举报
回复
int* test( int *b ) 
{
001A1D10  push        ebp  
001A1D11  mov         ebp,esp  
001A1D13  sub         esp,0C0h  
001A1D19  push        ebx  
001A1D1A  push        esi  
001A1D1B  push        edi  
001A1D1C  lea         edi,[ebp-0C0h]  
001A1D22  mov         ecx,30h  
001A1D27  mov         eax,0CCCCCCCCh  
001A1D2C  rep stos    dword ptr es:[edi]  
	return b;
001A1D2E  mov         eax,dword ptr [b]  
}
int & test1(int *b)
{
001A1CD0  push        ebp  
001A1CD1  mov         ebp,esp  
001A1CD3  sub         esp,0C0h  
001A1CD9  push        ebx  
001A1CDA  push        esi  
001A1CDB  push        edi  
001A1CDC  lea         edi,[ebp-0C0h]  
001A1CE2  mov         ecx,30h  
001A1CE7  mov         eax,0CCCCCCCCh  
001A1CEC  rep stos    dword ptr es:[edi]  
	return *b;
001A1CEE  mov         eax,dword ptr [b]  
}
真相重于对错 2018-03-05
  • 打赏
  • 举报
回复
const int& findmax(const vector<int>& a) { int maxindex = 0; for(int i = 1; i < a.size(): ++i) { if(a[i]>a[maxindex]) { maxindex = i; } } return a[maxindex]; }
  • 打赏
  • 举报
回复
大神看看怎么还有错误啊 #include <iostream> #include <vector> using namespace std; const int& findmax(const vector<int>& a); int main() { vector<int> a = {1,2,3}; cout << findmax(a) << endl; } const int& findmax(const vector<int>& a) { int maxindex = 0; for(int i = 1; i < a.size(): ++i) { if(a[maxindex] < a[i]) { maxindex = i; } else { maxindex = i; } } return a[maxindex]; }
赵4老师 2018-03-05
  • 打赏
  • 举报
回复
指针即地址 引用即别名
真相重于对错 2018-03-05
  • 打赏
  • 举报
回复
你的代码有逻辑错误
#include <iostream>
#include <vector>

using namespace std;
//这里应该声明 findmax 
int main()
{
    vector<int> a={1,2,3)
    cout << findmax(a) << endl;
}

const int& findmax(const vector<int>& a)
{
           int maxindex = 0;
           for(int i = 0; i < a.size()-1; ++i) //i的范围是0 至 a.size()-1,所以应该是for( int i=0;i<a.size();++i )
          {
               if(a[i] < a[maxindex])
               {
                 i = maxindex; //这里应该是 maxindex=i;
                }
           }
           return a[i]; //这里应该return a[maxindex];
} 
  • 打赏
  • 举报
回复
上面的for循环里的i定义在上面
  • 打赏
  • 举报
回复
求大神让下面的代码编译通过,上面的错了 #include <iostream> #include <vector> using namespace std; int main() { vector<int> a={1,2,3) cout << findmax(a) << endl; } const int& findmax(const vector<int>& a) { int maxindex = 0; for(int i = 0; i < a.size()-1; ++i) { if(a[i] < a[maxindex]) { i = maxindex; } } return a[i]; }
  • 打赏
  • 举报
回复
求大神让下面的代码编译通过! #include<iostream> #include <vector> using namespace std; int main() { vector<int> a={1,2,3}; cout << findmax(a) << endl; } const int& fundmax (const vector<int>& a) { int maxindex = 0; for(int i = 1; i < a.size(); ++i) { if(maxindex[i]>a[i]) { maxindex = i; } return a[maxindex]; } }
真相重于对错 2018-03-05
  • 打赏
  • 举报
回复
引用 6 楼 majiayefengkuanghaha 的回复:
指针在参数列表中声明,不是局部变量吗?
对,但是它指向的地址,不是副本,拿你的函数来说 int *a = new int [10]; //这时候a ------------------------->一段地址 ..... int& get(int *array,int index) { //array 虽然是a 的副本 但是它同样指向a所指向的地址 return array[index] ,返回的并不是array本身,而是array指向的地址 }
  • 打赏
  • 举报
回复
指针在参数列表中声明,不是局部变量吗?
真相重于对错 2018-03-05
  • 打赏
  • 举报
回复
因为是输入参数就可以返回引用,这句话不完全对 比如 int & test(int a) { return a; } 这个是错误 ,因为这里a,是实参的副本,不能返回引用 你的代码可以返回,因为它是个指针。 把上边的代码 改成 int & test(int& a) { return a; } 是对的
L-talent 2018-03-05
  • 打赏
  • 举报
回复
#include <iostream> using namespace std; void get(int a, int b) { if (a > b) a++; else b++; } void main() { int a = 2, b = 3; get(a, b); cout << a<<endl<<b<<endl; system("pause"); }
paschen 版主 2018-03-05
  • 打赏
  • 举报
回复
返回局部变量指的是要返回这个函数中定义的变量,由于这些变量在函数结束后就销毁了,导致之后的行为未定义,但你这里并不是这样的情况。
加载更多回复(4)

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧