开发者

Arithmetic with void pointers in C++

I need to access an object in a buffer, pointed by a void pointer. The object开发者_高级运维 is located at a certain offset but since arithmetic on a void pointer is prohibited how can I access the object?


You can cast the pointer to char* (+1 on such pointer is offset by one byte) or any other pointer type if that suits your needs better.

However, this approach is grossly error prone! You better check your design, something smells here! void* are in 99% of cases unnecessary in C++, designs that use them are usually more "C" than "C++". Remember, templates and inheritance should be the way to do these things.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜