Void pointer

Hi,

I have a query regarding void pointer.
Can any explain what happen when below snippet runs.

#include <iostream>

using namespace std;

class base
{
private:
int a;
public:
base()
{
a =9;
}
virtual void ini() = 0;

};

class derived1:public base
{
private:
int d1;
public:
derived1()
{
d1 = 8;
}
void ini()
{
cout<<"derived1 : "<<d1;
}
};

class derived2:public base
{
private:
int d2;
public:
derived2()
{
d2 = 7;
}
void ini()
{
cout<<"derived2 :"<<d2;
}
};
static void fun(void *vP)
{
base *b = (base *) (vP);
b->ini();
}
int main()
{
cout << "Hello World" << endl;
base *bd1 = new derived1();
fun(bd1); // output is : derived1 : 8
base *bd2 = new derived2();
fun(bd2); // output is : derived2 : 9
return 0;
}

Is this the corret way to do. In void fun i have no idea about which derived class void pointer is pointing to.

Many thanks.
s this the corret way to do. In void fun i have no idea about which derived class void pointer is pointing to.

No. This is not the correct way to do it. You could as easily supply the function with a pointer to a completely unrelated type and it would happily barf undefined behavior all over you.

fun should take a pointer-to-base as the type of the parameter.
Hi cire,
Thanks for your feed back. If Im sure that void ptr to that function will particular base pointer. Is it safe to use.? And more over we cant use dynamic cast on void pointer. What could be the good way to typecast void pointer?
Last edited on
If Im sure that void ptr to that function will particular base pointer. Is it safe to use.?

As cire said, this is not the correct way to do this. Typecasting a void pointer is never a good idea.

Is there some reason fun can't take a pointer to base?
1
2
3
static void fun (base *b)
{   b->ini();
}


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Hi,
Thanks and Sure i will follow the formatting.

Is there some reason fun can't take a pointer to base?

It has to be like callbacks.
I have 2 dlls. Lets say A.dll and B.dll. A.dll loads B.dll.
This static function is in A.dll, where this will be invoked from B.dll as a call back via function Pointer.

A.dll
1
2
3
4
static void fun(void *vPtr)
{
 typecast and use.
} 



B.dll

functionPtr( void Vptr)
function ptr points void fun
Vptr points to base class .

So i cant have base pointer in static function.
Is there any other way to solve.?
You can cast from derived to base (which you've already done) and just pass base. So...

static void fun(base *yo) { /* code here */ }

Actually, this has already been explained above a few times. Please read more carefully.
Last edited on
Hi ,
Thanks . Sorry I don't get you. My requirement is static function should accept void pointer as parameter.
Why is that a requirement?
Last edited on
Hi,
Thanks. Thats what i explained in previous replies.
Here is it.

It has to be like callbacks.
I have 2 dlls. Lets say A.dll and B.dll. A.dll loads B.dll.
This static function is in A.dll, where this will be invoked from B.dll as a call back via function Pointer.

A.dll
1
2
3
4
static void fun(void *vPtr)
{
 typecast and use.
} 




B.dll
In B.dll function will be invoked like below.

functionPtr( void Vptr)
function ptr points void fun
Vptr points to base class .

So i cant have base pointer in static function.
Is there any other way to solve.?
What is the problem with making base visible in A.dll?
That way you can pass a pointer to base in fun().

Anything else breaks the type safety of C++.
Hi,
I need to declare base pointer (which is defined in A.dll) in B.dll.
A.dll loads B.dll. Are you saying to use A.dll's header files in B.dll also.
Will it not become cyclic dependency.?
Will it not become cyclic dependency.?

No.

A.h defines base.
A.cpp includes A.h
B.h includes A.h
B.h defines derived1 and derived2.
B.cpp includes A.h and B.h

base is visible to both A.cpp and B.cpp.
derived1 and derived2 are visible only to B.cpp.
There is no circular dependency.

This is how interfaces are often handled, especially when the DLL is loaded dynamically. i.e. The base (interface) class is visible to applications, while the derived class can depend on which DLL is loaded.
Last edited on
Thanks. Here thing is that A.cpp includes B.h also.
Topic archived. No new replies allowed.