In C++ on Win32:
Suppose I have a DLL with a header file that declares a class. The DLL exports some means of obtaining a pointer/reference to an instance of that class, such as a factory function.
Am I correct in believing that it is not necessary to mark that class as exported using __declspec if one is only going to call virtual or inline functions on its instances?
Conversely, is it necessary to export the class declaration if one wishes to call nonvirtual member functions?
-
It's not necessary only if the function/class have all it's definition with in a header file. It's not dependant on virtuality.
So, if you don't export all your class, it's usable by client code as far as it don't have any public or protected function definition in a cpp file.
You can also declare only specific member functions to be exported instead of the whole class, by using __declspec in the function declaration and not in the class name declaration.
ChrisW : "It's not dependant on virtuality" It's not? If a function is virtual then it's invoked via the vptr/vtable. COM objects, for example, don't export every (or even any) method implementation.jmucchiello : Yes, but COM objects export the vptr table itself.ChrisW : No they don't: instead the COM object itself contains a pointer to its own vtable (so anyone who has a COM object instance doesn't need to be linked to the COM object's exported functions, or even linked to its vtable). -
Am I correct in believing ...
Yes I think so, but:
You should test that (I can't at the moment)
You might want to beware using inline methods: because if you change them later then you ought to rebuild every other component which depends on (has been built with) this class (i.e. the DLL is no longer at all well-insulated or self-contained ... inline methods are OK within a single DLL, but conducive to a kind of 'DLL hell' if used in a DLL's exported interface).
Conversely, is it necessary to export the class declaration if one wishes to call statically defined member functions?
If not the whole class, you need to export at least those individual static methods.
-
Am I correct in believing that it is not necessary to mark that class as exported using __declspec if one is only going to call virtual or inline functions on its instances?
Yes,this is correct, and that's what COM do, the DLL only expotys 4 methods, one of them returns to the class factory, which all its members are pure virtual functions.
Conversely, is it necessary to export the class declaration if one wishes to call statically defined member functions?
No, just export the static member functions.
-
C++ name mangling serves as a burden to the success of writting cross-compiler modulars, simply declare the class you want to expose as an interface containing nothing but virtual functions. The layout of the class with virtual functions might be 'standardized', think of COM.
0 comments:
Post a Comment