When to use a void pointer?

后端 未结 13 2022
北荒
北荒 2020-12-07 17:33

I understand the use of void pointer for malloc implementation.

void* malloc  ( size_t size );

Can anyone suggest other reasons or provide

13条回答
  •  感动是毒
    2020-12-07 18:18

    Next to interfacing with C, I find myself only using void pointers when I need to debug / trace some code and like to know the address of a certain pointer.

    SomeClass * myInstance;
    // ...
    std::clog << std::hex << static_cast< void* >(myInstance) << std::endl;
    

    Will print something like

    0x42A8C410
    

    And, in my opinion, nicely documents what I'm trying to do (know the pointer address, not anything about the instance)

提交回复
热议问题