0

The standard implementation of the pImpl idiom puts the following code in the .h file:

class MyClassImpl;

class MyClass
{
public:
    MyClass();
    ~MyClass();
    MyClass(const MyClass&);
    MyClass& operator=(const MyClass&);

private:    
    MyClassImpl* pImpl;
};

But, this requires the constructors and destructors to be provided to the user of my library in a form of a .LIB file (static library). However, I want to create a DLL library that does not require the user to link with additional static libraries. There are some funcions like this:

__declspec(dllexport)
void MyFunction(MyClass& object);

And the user needs to create the object before passing it to the function. Thus, he needs access to the constructor and destructor.

What is the most correct way to design the API of such library?

Michal Czardybon
  • 2,795
  • 4
  • 28
  • 40

1 Answers1

0

If the user is allowed to play with MyClass instances, (what will prevent him to put them in STL container?, or using other STL types like shared_ptr?) I don't see how you can make it without exporting the whole class.

See Using dllimport and dllexport in C++ Classes

Nevertheless, you may hit the "multiple CRT" problem. You should clarify your intend, that is:

  1. what kind of user/program?
  2. why do you need the "DLL and plimp" approach?

If your intent is to provide a DLL used in unknown various contexts (commercial DLL), you should use an opaque type (kind of HANDLE), using a "create/delete" API, NOT "publishing" the MyClass class.

manuell
  • 7,528
  • 5
  • 31
  • 58
  • pImpl does not preclude putting the objects into STL containers etc. There is a copying constructor and the assignment operator - that should be enough. The "multiple CRT" problem does not appear, because both construction and destruction is performed inside the library. And I need it in this way, because we are creating a commerial DLL library product. – Michal Czardybon Nov 28 '13 at 11:49
  • If you allow the EXE to play with instances, construction and destruction will be done with the EXE crt version of new/delete – manuell Nov 28 '13 at 12:03