将函数输出直接传递给接受引用参数的函数?
这是我的第一篇文章,我希望我做得很好。我已经尝试解决这个问题有一段时间了,但同时只是寻找一个中间变量。嗯,这就是我的意思:
//from a pre-built library
double getValue(int idx)
{
//Returns some value from within a class
}
//from a function I created
void setValue(double &input)
{
//set some value here
}
我目前正在按如下方式执行我的程序:
double numberOne;
numberOne = getValue(0);
setValue(numberOne);
这可以工作并编译。然而,我想做一些如下的事情:
setValue(getValue(0));
但是我似乎无法做到正确(已经尝试了一些引用\取消引用的事情,但我只是在黑暗中拍摄)。 我想知道是否可以这样做?此外,如果可以这样做,以这种方式执行它是否比使用中间存储值(又名 numberOne)有任何速度\内存空间优势。对于双精度类型值来说,这确实不是问题,但是当它是一个占用空间更大的类时,出于速度\内存考虑,我想尽可能减少内存使用量\深度复制。
顺便说一句,是否有任何书籍或在线资源可以帮助我加快 C++ 程序的速度,并提高速度\内存使用的其他效率。
预先非常感谢您能够提供的任何帮助。
This is my first post and I hope I'm doing it ok. I have tried to solve this problem for a while now but just went for an intermediary variable in the meantime. Well this is what I mean:
//from a pre-built library
double getValue(int idx)
{
//Returns some value from within a class
}
//from a function I created
void setValue(double &input)
{
//set some value here
}
I am currently doing my program as follow:
double numberOne;
numberOne = getValue(0);
setValue(numberOne);
This works and compiles. I would like to however do something as follows:
setValue(getValue(0));
However I can't seem to get it right (have tried a number of referencing\de-referencing things but I'm just shooting in the dark).
I would like to know if it's even possible to do so? Furthermore if it is possible to do so, is there any speed\memory space advantages to perform it this way rather than having an intermediary storage value (aka numberOne). It really isn't a concern for a type double value but when its a class with a much larger footprint then I would like to reduce the amount of memory usage\deep copying as much as possible for speed\memory considerations.
On a side note are there any books or online resources that can help me to speed up my C++ programs with other efficiency improvements to speed\memory usage.
Thanks so much in advance for any help you may be able to provide.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将此 : 更改
为
毕竟,
input
应该真正是输入,因此setValue()
应该这样对待。如果您按值传递,这里的性能不会更糟。但是,对于大型类,您可能希望将其更改为引用避免复制并实现更好性能:Change this :
to
After all,
input
should truely be input, and sosetValue()
should treat this as such. Here performance is not even worse if you pass by value. However, for big classes, you might want to change it to reference to avoid copying and to achieve better performance:函数的返回值是临时的。有一条规则是不能将临时引用绑定到非常量引用,因为它会导致很多错误。
不过,您可以将临时值绑定到 const 引用,例如
这对于更大或更复杂的类型很有用,但对于像 double 这样的基本类型,您也可以按值传递它
The return value from the function is a temporary. There is a rule that you cannot bind a temporary to a non-const reference, because it turned out to cause a lot of errors.
You can bind the temporary to a const reference though, like
This can be good for larger or more complex types, but for primitive types like double you can just as well pass it by value
您转换您的设置值:
这应该可以解决问题。
如果你的“double”足够大(在这里它是无用的),那么获得参考是有好处的。这样,您将仅在
setvalue()
中复制您感兴趣的对象部分。You transform your setvalue :
That should remove the problem.
If your "double" is big enough (here it is useless), there are advantages getting a reference. This way you will only copy the part of the object that interest you in the
setvalue()
.