I have an unmanaged Win32 C++ application that uses multiple C++ DLLs. The DLLs each need to use class Foo – definition and implementation.
Where do Foo.h and Foo.cpp live so that the DLLs link and don’t end up duplicating code in memory?
Is this a reasonable thing to do?
[Edit]
There is a lot of good info in all the answers and comments below – not just the one I’ve marked as the answer. Thanks for everyone’s input.
Providing functionality in the form of classes via a DLL is itself fine. You need to be careful that you seperate the interrface from the implementation, however. How careful depends on how your DLL will be used. For toy projects or utilities that remain internal, you may not need to even think about it. For DLLs that will be used by multiple clients under who-knows-which compiler, you need to be very careful.
Consider:
If
MyGizmois going to be used by 3rd parties, this class will cause you no end of headaches. Obviously, the private member variables are a problem, but the return type forget_name()is just as much of a problem. The reason is becausestd::string‘s implementation details are part of it’s definition. The Standard dictates a minimum functionality set forstd::string, but compiler writers are free to implement that however they choose. One might have a function namedrealloc()to handle the internal reallocation, while another may have a function namedbuy_node()or something. Same is true with data members. One implementation may use 3size_t‘s and achar*, while another might usestd::vector. The point is your compiler might thinkstd::stringis n bytes and has such-and-so members, while another compiler (or even another patch level of the same compiler) might think it looks totally different.One solution to this is to use interfaces. In your DLL’s public header, you declare an abstract class representing the useful facilities your DLL provides, and a means to create the class, such as:
DLL.H :
…and then in your DLL’s internals, you define a class that actually implements
MyGizmo:mygizmo.cpp :
This might seem like a pain and, well, it is. If your DLL is going to be only used internally by only one compiler, there may be no reason to go to the trouble. But if your DLL is going to be used my multiple compilers internally, or by external clients, doing this saves major headaches down the road.