参考未返回
Reference not returning?
我的类有一系列对象,称其为 Foo
。它在同类中存储为Foo* m_Foos
。假设它具有[0]的值,并且保证和Foo
具有称为IsSet
的属性,它只是一个bool或其他东西。
void TryThis()
{
Foo returnValue;
GetValue(returnValue);
returnValue.IsSet = true;
if(m_Foo[0].IsSet != returnValue.IsSet)
{
// ERROR!!!!
}
}
void GetValue(Foo &container)
{
container = m_Foos[0];
}
谁能解释为什么m_foo [0] =/= returnValue?我的语法中的错误在哪里?
我希望m_foo [0]与returnValue相同,在内存中相同的 Foo
。
TryThis()
没有修改存储在m_Foos
数组中的Foo
对象。
GetValue()
将Foo
对象从m_Foos[0]
分配给TryThis()
本地的另一个Foo
对象。在该评估期间正在制作副本。然后,TryThis()
正在修改副本,而不是原始副本。
如果您希望TryThis()
直接修改原始Foo
对象,则需要做更多这样的事情:
void TryThis()
{
Foo &returnValue = GetValue();
returnValue.IsSet = true;
// m_Foo[0] is set true.
}
Foo& GetValue()
{
return m_Foos[0];
}
或以下:
void TryThis()
{
Foo *returnValue;
GetValue(returnValue);
returnValue->IsSet = true;
// m_Foo[0] is set true.
}
void GetValue(Foo* &container)
{
container = &m_Foos[0];
}
相关文章:
- 赋值运算符的返回值可以作为参考吗?
- C 标准:通过复制返回以初始化无RVO的参考:是否有任何副本
- JNI返回Java对象,可以返回本地参考,还是必须是全局
- 是否可以通过引用通过参数返回参考
- 返回int数据类型v/s传递参考
- 通过C 中的参考返回结果
- 在C 中返回参考有效期参考是否有效
- 通过参考返回C 中的对象
- 为什么const rvalue合格的std ::可选:: value()返回const rvalue参考
- 转发参考:给定 T&& 时返回 T&& 和给定 T&&时返回 T&
- C 通过参考返回本地对象
- C 按值返回与参考返回
- 参考未返回
- 分配运算符过载:返回void与返回参考参数
- 分配给返回参考的函数
- 使用Ternery表达式返回参考会导致运行时错误
- 带有get函数的模板类始终返回参考
- 数组[]运算符返回参考,因此如何更改其值
- 返回参考时返回错误
- 返回参考资料在这里是如何工作的