Reputation: 929
In language like C#. you can put public or internal in front of a class to control the access level of a class. How this is done in an C++ DLL?
Upvotes: 0
Views: 310
Reputation: 1444
Unlike C#(or any other modern languages) C++ language standard doesn't talks anything about Dynamic Libraries/Shared Objects.
So it is all specific to the language/compiler implementors to define their own way of exporting class definitions across DLL/SO.
Refer http://gcc.gnu.org/wiki/Visibility for more info.
#if defined _WIN32 || defined __CYGWIN__
#ifdef BUILDING_DLL
#ifdef __GNUC__
#define DLL_PUBLIC __attribute__ ((dllexport))
#else
#define DLL_PUBLIC __declspec(dllexport) // Note: actually gcc seems to also supports this syntax.
#endif
#else
#ifdef __GNUC__
#define DLL_PUBLIC __attribute__ ((dllimport))
#else
#define DLL_PUBLIC __declspec(dllimport) // Note: actually gcc seems to also supports this syntax.
#endif
#endif
#define DLL_LOCAL
#else
#if __GNUC__ >= 4
#define DLL_PUBLIC __attribute__ ((visibility ("default")))
#define DLL_LOCAL __attribute__ ((visibility ("hidden")))
#else
#define DLL_PUBLIC
#define DLL_LOCAL
#endif
#endif
extern "C" DLL_PUBLIC void function(int a);
class DLL_PUBLIC SomeClass
{
int c;
DLL_LOCAL void privateMethod(); // Only for use within this DSO
public:
Person(int _c) : c(_c) { }
static void foo(int a);
};
From the above snippet you can observe that exporting class is directly dependent on the compiler specific extension.
CL/MSVC - __declspec(dllexport), __declspec(dllimport
GCC - __attribute__ ((visibility ("default")))
GCC also provide **-fvisibility=[hidden|default] to have a more control over the symbols which is been exported over DLL/SO.
Upvotes: 0
Reputation: 490108
Presumably you're basically asking how to export a class from a DLL.
In that case, most compilers for Windows (e.g., VC++, MinGW) use __declspec(dllexport)
to do the job.
To mirror that, the client codes need to declare the class as __declspec(dllimport)
. Typically you end up with something like:
#ifdef BUILD_DLL
#define DLL __declspec(dllexport)
#else
#define DLL __declspec(dllimport)
#endif
DLL class whatever {
// ...
};
...in the header, then the make file for building the DLL will define BUILD_DLL
:
cflags += /DBUILD_DLL
Upvotes: 1
Reputation: 794
By nesting one class inside another:
class A
{
public:
class B {};
protected:
class C {};
private:
class D {};
};
Upvotes: 0