gpt4 book ai didi

c++ - 什么会导致 `MyType *pType` 在返回时从有效参数变为 null?

转载 作者:搜寻专家 更新时间:2023-10-31 01:16:22 26 4
gpt4 key购买 nike

考虑这个方法:

result MyClass::getBMPText(Osp::Graphics::Bitmap *pBMP, Osp::Base::String &outtext, const int index) const {
//Do stuff
AppLog("3 Returning %S, 0x%X", outtext.GetPointer(), (int)pBMP);
return E_SUCCESS;
}

我这样调用它:

String itemstr;
Bitmap *pBMP = null;
for (int i = 0; i < ItemCount(); ++i) {
getBMPText(pBMP, itemstr, i);
AppLog("got %d : %S 0x%X", i, itemstr.GetPointer(), (int)pBMP);
}

现在看一下日志:

5537.642,INFO,P35,T00,A190,FileMan::getBMPText (401) > 3 Returning Images, 0xB96E2140 5537.643,INFO,P35,T00,A190,FileMan::Update1p2List (130) > got 0 : Images 0x0

重复我的问题/观察:该函数将其返回值记录为有意义且与刚刚设置的相关。然而,客户端会返回它发送的相同空引用。

最佳答案

您按值传递指针,因此原始指针永远不会改变。将函数签名更改为 Osp::Graphics::Bitmap * & pBMP 以通过引用传递指针。

关于c++ - 什么会导致 `MyType *pType` 在返回时从有效参数变为 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9137290/

26 4 0