Question

  1. I have a C++ class Alice that must be exported from a dll. It has references to objects of other custom classes like Bob.

  2. When I use my dll in another exe project I don't want and don't need to know about Bob (even if the reference is public).

  3. I thought of making something like HANDLE in WinApi out of Bob's objects (like IntPtr in C#).

Reasons: I need to include Alice.h in my exe project to be able to use Alice right ? So I want to expose only 1 header file and not Bob.h and Chellsie.h and Detlef.h and ...


How can I achieve this ?
Is HANDLE a good idea ? Or can I avoid my problem below in a different way ?
Code:

class __declspec(dllexport) Alice
{
public:
    Bob bob;
    Chellsie chellsie;
    Detlef detlef;
};

class __declspec(dllexport) Alice
{
public:
    HANDLE bob;
    HANDLE chellsie;
    HANDLE detlef;
};
Was it helpful?

Solution

Sure Alice needs Bob, Bob is an integral part of Alice. It's composition. If you held a pointer to a "Bob", you'd be able to use a forward declaration, but as-is now, a full definition of Bob is required:

#include "Bob.h" // required, Bob must be known

class __declspec(dllexport) Alice
{
public:
    Bob bob;
    Chellsie chellsie;
    Detlef detlef;
};

vs

class Bob; // declaration is enough

class __declspec(dllexport) Alice
{
public:
    Bob* bob;
    Chellsie chellsie;
    Detlef detlef;
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top