
Greg Clayton wrote:
-----Original Message----- From: Peter Dimov
Isn't it possible for the DLL C that contains the definition of SomeClass to export
shared_ptr<SomeClass> createSomeClass();
which DLL B can now call when it needs a SomeClass?
Since ~SomeClass lives in C, it must be available when the last shared_ptr<SomeClass> is destroyed.
This was my initial solution, and it does work.
I mention it because it's the recommended approach, especially if SomeClass is most frequently allocated on the heap and used via shared_ptr<SomeClass> (there are no automatic variables of type SomeClass, and so on). Hiding the actual 'new SomeClass' behind an exported factory function makes SomeClass less error-prone, because users no longer need to use 'new' directly. You can make the constructors and the destructor of SomeClass protected to further prevent accidental mistakes. You'll also be able to change the way a SomeClass is allocated without changing one bit of code outside the DLL that hosts 'SomeClass::create' or 'createSomeClass'.
Though I was searching for the ability to just create a shared pointer anywhere and had it to any other DLL by exporting the entire shared pointer instantiation from the main DLL.
What does this buy you? Whether you export shared_ptr<SomeClass>::shared_ptr<SomeClass> or createSomeClass doesn't make much difference, and, at least in my opinion, it creates more trouble than it's worth. But I may be missing something.