[英]How to return reference to variable with single return statement
Consider the following snippet. 考虑以下代码段。
MyType_t& Variable(int i) {
if (i == 0) {
return MyVariable0;
}
if (i == 1) {
return MyVariable1;
}
return MyDefaultVariable;
}
This will return a reference to a variable that can then be modified. 这将返回对变量的引用,然后可以对其进行修改。
Variable(1) = 10;
However, our coding standard says to use a single point of return. 但是,我们的编码标准要求使用单个返回点。 How would I accomplish this?
我将如何完成? For some reason, the following seems to only copy the value (thus not returning a reference to the root variable), even though I've verified stepping through code it sets
var
to the appropriate variable. 出于某种原因,即使我已经验证了将代码将
var
设置为适当变量的逐步方法,以下内容似乎也只会复制该值(因此不会返回对根变量的引用)。 This would be simple with pointers, but I just can't get the references right. 使用指针将很简单,但我只是无法正确获得引用。
MyType_t& Variable(int i) {
MyType_t& var = MyDefaultVariable;
if (i == 0) {
var = MyVariable0;
}
if (i == 1) {
var = MyVariable1;
}
return var;
}
Notes 笔记
The returned variable has a global scope (its on an embedded system). 返回的变量具有全局范围(在嵌入式系统上)。
The function is used as a convenience method inside a class, with very limited use/scope. 该函数用作类内部的便捷方法,使用/范围非常有限。
Your reference is bound to MyDefaultVariable
and you cannot modify it afterwards, because that's how it works. 您的引用绑定到
MyDefaultVariable
,以后您将无法对其进行修改,因为这是它的工作方式。 Your var = MyVariable0;
您的
var = MyVariable0;
effectively called MyDefaultVariable
's copy operator, which results in the behavior you observed. 有效地调用
MyDefaultVariable
的复制运算符,从而导致您观察到的行为。
If you want to avoid using a ternary operator here, you can use something close to what you suggested, but using a pointer later dereferenced within the return statement. 如果要避免在此处使用三元运算符,则可以使用与建议的内容接近的内容,但可以使用稍后在return语句中取消引用的指针。
MyType_t& Variable(int i) {
MyType_t* var = &MyDefaultVariable;
if (i == 0) {
var = &MyVariable0;
}
if (i == 1) {
var = &MyVariable1;
}
return *var;
}
If you want your function to remain readable while adding more variables, I would discourage the use of reference/ternary operators. 如果您希望函数在添加更多变量时保持可读性,则不建议使用引用/三元运算符。 Pointers would be the way to go.
指针将是必经之路。
MyType_t& Variable(int i)
{
MyType_t* var = &MyDefaultVariable;
if (0 == i)
var = &MyVariable0;
else if (1 == i)
var = &MyVariable1;
return *var;
}
You could use the conditional operator : 您可以使用条件运算符:
MyType_t& Variable(int i) {
return ( i==0 ) ? MyVariable0 : ( i==1) ? MyVariable1 : MyDefaultVariable;
}
However, a coding standard that forces you to write unreadable code does not deserve to be called "coding standard" imho. 但是,强迫您编写不可读代码的编码标准不应该被称为“编码标准”恕我直言。
std::reference_wrapper will provide this functionality without exposing you to the risk of raw pointers. std :: reference_wrapper将提供此功能,而不会使您暴露于原始指针的风险。 using reference_wrappers also expresses intent.
使用reference_wrappers也会表达意图。
Note that assigning a new reference to a std::reference_wrapper will cause the wrapper to rebind, it will not overwrite the contents of the referred-to variable. 请注意,为std :: reference_wrapper分配新的引用将导致包装器重新绑定,它不会覆盖所引用变量的内容。
#include <functional>
using MyType_t = int;
extern int& MyDefaultVariable;
extern int& MyVariable0;
extern int& MyVariable1;
MyType_t& Variable(int i) {
auto var = std::ref(MyDefaultVariable);
if (i == 0) {
var = MyVariable0;
}
if (i == 1) {
var = MyVariable1;
}
return var;
}
Another way to express this would be: 另一种表达方式是:
MyType_t& Variable(int i)
{
auto var = std::ref(MyDefaultVariable);
switch(i)
{
case 0: var = MyVariable0; break;
case 1: var = MyVariable0; break;
default: break;
}
return var;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.