Re: void * pointers
Rahul wrote:
> Hi Everyone,
>
> There was a discussion on the need of void * in C and C++. In C, it
> is a generic pointer which can be typecasted to and from that of other
> types.
>
> And it is developer's head ache to take care that the cast to and
> from void * is correct. C++ enhanced type cast dynamic_cast doesn't
> in this case, as it needs the type information. This makes me
> wonder, why make a cast to void * and pass the info to other
> functions? Why not simply pass the direct pointer?
>
> Does anyone know the reason where void* only can solve the problem?
In C generic programming is made by using void pointers and id
parameters. In C++ we use templates instead:
For example:
/*C */
void somefunc(void *p, char id)
{
if (id=='i')
/* ... */
if (id== 'n')
/* ... */
}
// C++
void somefunc(T &obj)
{
//...
}
|