参考不返回?
Reference not returning?
我的 class 有一个对象数组,称之为 Foo
。它在 class 中存储为 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()
正在将 m_Foos[0]
中的 Foo
对象分配给 TryThis()
本地的另一个 Foo
对象。 copy 正在该分配期间进行。 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];
}
我的 class 有一个对象数组,称之为 Foo
。它在 class 中存储为 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()
正在将 m_Foos[0]
中的 Foo
对象分配给 TryThis()
本地的另一个 Foo
对象。 copy 正在该分配期间进行。 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];
}