- 1 year ago
Can I generate DLL without exporting friend classes
Yes, it is possible to generate a DLL without exporting friend classes. In C++, friend classes and functions are a mechanism to grant access to private or protected members of a class to another class or function. However, marking a class as a friend does not automatically export it in a DLL.
By default, only functions and classes marked with the __declspec(dllexport)
attribute or added to an explicit export definition file (e.g., .def
file) are exported from a DLL. Friend classes that are not explicitly marked for export will not be accessible from outside the DLL.
Here's an example to illustrate this:
cpp// MyClass.h (inside the DLL project)
class __declspec(dllexport) MyClass {
private:
int privateMember;
public:
void SomeMethod();
friend class FriendClass;
};
class FriendClass {
public:
void AccessPrivateMember(MyClass& obj);
};
In this example, the MyClass
is marked for export using __declspec(dllexport)
, but the FriendClass
is not. Only the members of MyClass
will be accessible from outside the DLL.
If you have a friend class that you do not want to be accessible outside the DLL, simply do not mark it for export. It will be treated as a regular class and will not be visible to other modules or projects.
Keep in mind that this applies to the Windows platform and the Microsoft Visual C++ compiler. If you are using a different compiler or platform, the mechanisms for exporting symbols from a DLL may vary.