Re: member function level dll export
On Thu, 24 Apr 2008 13:10:58 -0700 (PDT), qdin <eryqdin@gmail.com> wrote:
Thanks for the quick response.
I impelement them in a class CFoo that inherits from IFoo, so the
functions are defined there.
Your answer explains why I don't see them in depends, but not why when
I call into this dll from an app, it makes it into the appropriate
function.
in my app:
IFoo * pFoo = Factory::CreateFoo();
pFoo->Func1();
Factory is a class in my dll too.
CreateFoo is a static member func that internally news a CFoo, and
returns a casted pointer to IFoo.
When I call pFoo->Func1() it does make it into CFoo::Func1(). How does
it jump into the dll?
A class that has virtual functions also has a data structure called a
"vtbl", or "virtual function table", and each object of the class has a
hidden pointer member, the "vptr", which points to the class's vtbl. The
vtbl is an array of function pointers, and when you say:
p->f(); // f is virtual
The compiler implements it something like this:
(*p->vtbl[index_of_f])()
From the caller's perspective, this does not require the function's name to
be known to the linker. Of course, to fill the vtbl with function
addresses, the function's name has to be known to the module that creates
the vtbl. Putting this together, as long as the creational aspects happen
in the DLL, the DLL users can call virtual functions without you
dllexporting them. However, if you disable the dynamic call mechanism by
calling the function statically, e.g. by saying base::f() or
p->ClassName::f(), the linker will need to be able to find the function by
name, and your DLL will need to dllexport them in order for its clients to
call them in this way.
NB: If you're uncertain of the purpose of the vtbl, it looks like this will
help:
http://www.parashift.com/c++-faq-lite/virtual-functions.html
If you can find a copy, the ARM (C++ Annotated Reference Manual by
Stroustrup) has a good description of a possible implementation of this and
many other things.
--
Doug Harrison
Visual C++ MVP