开发者

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

开发者 https://www.devze.com 2023-01-26 02:33 出处:网络
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开发者_Go百科 why we do it in the first pl

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开发者_Go百科 why we do it in the first place ?


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消