什么可能导致“MyType *pType”在返回时从有效参数变为 null?

发布于 2025-01-02 22:28:47 字数 754 浏览 1 评论 0原文

考虑这个方法:

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 返回图像,0xB96E2140 5537.643,INFO,P35,T00,A190,FileMan::Update1p2List (130) >得到 0:图像 0x0

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

Consider this method:

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;
}

I call it like this:

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);
}

Now take a look at the log:

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

To repeat my question/observation: The function logs its return value as meaningful and relevent having just set it. The client however gets back the same null reference it sent in.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长伴 2025-01-09 22:28:47

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

You're passing the pointer by value, so the original pointer never gets changed. Change the function signature to Osp::Graphics::Bitmap * & pBMP to pass the pointer by reference.

多像笑话 2025-01-09 22:28:47

如果您想更改原始指针,则必须将其作为指针或引用传递。例如。函数 getBMPTest 将被声明为

result MyClass::getBMPText(Osp::Graphics::Bitmap *& pBMP, Osp::Base::String &outtext, const int index) const

并被调用,

getBMPText(pBMP, itemstr, i);

如果您更喜欢指针,您可以这样做:

result MyClass::getBMPText(Osp::Graphics::Bitmap ** pBMP, Osp::Base::String &outtext, const int index) const

getBMPTest(&pBMP, itemStr, i);

If you'd like to change the original pointer, you'll have to pass it as a pointer or as a reference. For example. the function getBMPTest would be declared as

result MyClass::getBMPText(Osp::Graphics::Bitmap *& pBMP, Osp::Base::String &outtext, const int index) const

and called like

getBMPText(pBMP, itemstr, i);

If you prefer pointers, you can do it like this instead:

result MyClass::getBMPText(Osp::Graphics::Bitmap ** pBMP, Osp::Base::String &outtext, const int index) const

getBMPTest(&pBMP, itemStr, i);
半透明的墙 2025-01-09 22:28:47

这是因为您没有在函数内设置 *pBMP (您设置了 pBMP,指针类型)。更改指针本身不会影响其目标。

同样,使用 index 时,您是按值传递的,因此不会产生任何结果。

This is because you're not setting *pBMP inside the function (you set pBMP, the pointer type). Changing the pointer itself won't affect its target.

Likewise, with index, you're passing by value, so nothing comes out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文