whats the use of void pointers?

S

sam

Hello
whats the use of void pointers?
and when they are useful (on which conditions?)
Please give me example code with some explanation.
Thanks in advance.
 
V

Victor Bazarov

sam said:
whats the use of void pointers?

Allocation functions return pointers to void. Deallocation functions
take pointers to void. That makes me think that general dynamic
memory mechanisms cannot really avoid using pointers to void.
and when they are useful (on which conditions?)

They are useful when they are necessary. If you can avoid using them,
do.
Please give me example code with some explanation.

In most cases those are left for some C API compatibility where some
functions (like qsort) are written to work with "any object". Same
applies to your own design -- if you don't know what object you're
going to work with at run time, you might want to use pointers to void.
The most common example is a generic callback function. When you are
supposed to give your system a function to call and you want to use
unrelated classes and their member functions for that, a static member
with an extra argument of type void* may be just the solution for you.

V
 
S

sam

means we can use void pointer on objects and functions also
But is it necessay that function have to be a member function and that
object haveto be of class that is be parent class or the friend class
will also do
 
V

Victor Bazarov

sam said:
means we can use void pointer on objects and functions also

Uh... Yes.
But is it necessay that function have to be a member function and that
object haveto be of class that is be parent class or the friend class
will also do

Huh? Non-static member functions cannot be used.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top