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?
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
staticvoid 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.
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
staticvoid 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.?
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
staticvoid 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.?
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.?
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.