Atul
Atul

Reputation: 765

why do we need to export a class in C++?

I am a beginner, so, please bear with me, if this sounds too trivial.When i searched over the net for this, i got results showing how to do it. My question is why we do it in the first place ?

Upvotes: 4

Views: 1169

Answers (3)

Dynite
Dynite

Reputation: 2383

Technically you cannot export a class, only functions. However you can designate on a class level that all functions be exported.

Exporting a function means that that function can be called from outside of the current executable.

This is needed when you are writing a dll for example which is a separate entity.

Upvotes: 3

SirDarius
SirDarius

Reputation: 42869

This is specific to the Windows platform, when you're developping C++ DLLs.

You have to use the __declspec(dllexport) modifier in order for your class and its methods to appear on the exported symbol list for your DLL.

This way, executables using your DLL can instanciate and call methods in these classes.

However you have to make sure that the executable and the DLL are compiled by the same version of the same compiler, because C++ symbols are exported using a relatively complex name mangling encoding (you can see that using depends.exe), which format varies from one compiler to another.

Upvotes: 5

Arnout
Arnout

Reputation: 2790

You don't need to export anything, unless you're creating a DLL. In that case, you can use the dllexport attribute as an alternative to the "old school" way of using .def files.

Upvotes: 3

Related Questions