什么可能导致“MyType *pType”在返回时从有效参数变为 null?
考虑这个方法:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您按值传递指针,因此原始指针永远不会改变。将函数签名更改为 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.如果您想更改原始指针,则必须将其作为指针或引用传递。例如。函数
getBMPTest
将被声明为并被调用,
如果您更喜欢指针,您可以这样做:
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 asand called like
If you prefer pointers, you can do it like this instead:
这是因为您没有在函数内设置
*pBMP
(您设置了pBMP
,指针类型)。更改指针本身不会影响其目标。同样,使用
index
时,您是按值传递的,因此不会产生任何结果。This is because you're not setting
*pBMP
inside the function (you setpBMP
, the pointer type). Changing the pointer itself won't affect its target.Likewise, with
index
, you're passing by value, so nothing comes out.