Re: Switching between interfaces without knowing derived object.
hardy wrote:
On Aug 14, 7:25 am, Lars Tetzlaff <lars.tetzl...@gmx.net> wrote:
peter koch schrieb:
On 12 Aug., 23:48, Lars Tetzlaff <lars.tetzl...@gmx.net> wrote:
peter koch schrieb:
On 11 Aug., 20:20, hardy <hardy_hard...@yahoo.co.in> wrote:
Hello
Consider a case in which we have two interface classes I1 and I2 and a
derived Class D implementing both of these interfaces. This is a part
of the library and the library-user does not know the definition of
Class D. It knows only about I1 and I2. API's of this library return
pointers to interfaces, say I1 for this example. So if the user has a
pointer to object of type I1 and wants to convert it to I2*. Is there
a C++ standard way of converting it?
Yes - lookup std::dynamic_cast.
This is a solution, if you not only know about I1 and I2 but also that
the implementation of I1 implements I2, or, in other words, you need
*some* knowledge about Class D.
(...)
The gcc version i am using is 4.0.1 on Mandriva Linux release 2006. I
am able to replicate the problem in an isolated source. The problem,
i.e dynamic_cast returns NULL, occurs when the shared library is
linked and loaded at runtime.
While I cannot comment on the gcc / linux library loading problem I
think your code *may* have another problem ...
(...)
Sample Code.
This is testlib.cpp
#include "testlib.h"
class Derived : public Itf1, public Itf2
{
public:
virtual ~Derived(){}
void bar() { cout << "Bar" << endl; }
void foo() { cout << "Foo" << endl; }
};
#ifdef __cplusplus
extern "C" {
#endif
void* GetItf()
{
Derived* d = new Derived();
return static_cast<Itf1*>(d);
}
(...)
GetInterfaceFromLib = (void*(*)())dlsym(lib_handle, "GetItf");
(...)
Itf1* p = NULL;
void** pvoid = (void**)(&p);
*pvoid = (*GetInterfaceFromLib)();
p->foo();
(...)
delete p;
Here, you are deleteing a pointer obtained from the dynamic library.
I think this is not really portable, but I know that it generally works
on Windows DLLs -- i.e. on Win it will correctly call the dtor of the
Derived object, and will also correctly use the runtime library of the
lib to free the memory -- does it actually call the correct dtor on your
Linux? (And if, it seems even more weird to me that the dynamic_cast
should then fail ...)
br,
Martin
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]