由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - How to use a function return by reference in C++
相关主题
【包子求】BFGS-Matlab package同样的C++程序,Linux下比Windows下慢很多
tail call strange behavior on cl.exe问一个python读大文件的问题
C++ optimization questionMicrosoft Visual C++ compiler team is hiring
用C++的写的numerical or optimization solver libraryC++确实不适合做大项目
看了这篇文章,脑子有点不够用了scala大牛入
大家都用什么工具来profile C/C++程序a=b=0比a=0,b=0快
瓶颈在哪儿?a ANSI C question
震惊:java 的矩阵操作比 c++ 快?Do the two statements cost the same amount of time?
相关话题的讨论汇总
话题: func1话题: type1话题: function话题: return话题: c++
进入Programming版参与讨论
1 (共1页)
c**********e
发帖数: 2007
1
For example, I have a function
Type1& func1(int x)...
How can I call the function func1()?
c**********e
发帖数: 2007
2
I tried something like
Type1& x=func1(5);
It causes Bus error. Does anybody have an idea how to use
func1()? Thanks a lot.
c**********e
发帖数: 2007
3
Could anybody please helpppppppppppppppppppp?
h*******e
发帖数: 225
4
something is wrong with your function

【在 c**********e 的大作中提到】
: I tried something like
: Type1& x=func1(5);
: It causes Bus error. Does anybody have an idea how to use
: func1()? Thanks a lot.

c**********e
发帖数: 2007
5
Thanks. Which is the right way to call the function?
(a) Type1& x=func1(5);
(b) Type1 x=func1(5);
(c) Type1 x; x=func1(5);
(d) Type1& x; x=func1(5);

【在 h*******e 的大作中提到】
: something is wrong with your function
w******p
发帖数: 166
6
if ur function is like this:
Type1& func1(int x) {
Type1 ret;
return ret;
}
you are returning reference to local variable, after the call stack of func1
is wrapped up, ret is not guranteed to be valid anymore, hence bus error
may happen.
Just do
Type1 func1(int x) {
Type1 ret;
return ret;
}
modern compiler will prob. optimize off the copy in the return statement by
NRV (Named Return Value) optimization.
If you do need to have the func return by reference, you need to pass in the
referenc
1 (共1页)
进入Programming版参与讨论
相关主题
Do the two statements cost the same amount of time?看了这篇文章,脑子有点不够用了
好了。终于把3-way qsort完成标准优化了:)大家都用什么工具来profile C/C++程序
optimization瓶颈在哪儿?
about loop-invariant optimization震惊:java 的矩阵操作比 c++ 快?
【包子求】BFGS-Matlab package同样的C++程序,Linux下比Windows下慢很多
tail call strange behavior on cl.exe问一个python读大文件的问题
C++ optimization questionMicrosoft Visual C++ compiler team is hiring
用C++的写的numerical or optimization solver libraryC++确实不适合做大项目
相关话题的讨论汇总
话题: func1话题: type1话题: function话题: return话题: c++