开发者

How do you implement a wrapper function to change other function's return type?

Suppose such a function in a 3rd party library:

rttype func(...);

How do you write a wrapper function that functions开发者_高级运维 the same but only different in that its return type is rttype * instead of rttype?


Generally speaking, you don't. Going the other direction might be reasonable at times, but the minute you return a pointer you introduce questions of lifetime and ownership. In particular, what are you returning a pointer to, and whose responsibility (if anybody's) is it to free the memory it points at?

These questions typically affect the overall design, so they're not something you can reasonably address in a simple wrapper.


You will have to allocate global memory (memory on the heap), assign the rttype there and then return a pointer to the allocted memory. Of course, doing this you'll have to deal with freeing the memory later on too.

However, by doing this you are modifying the semantics and design of the function and you should bear this in mind and make other changes responsibly to respect this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜