Because when you're creating a larger dll (or shared library) you may want to compose it of several object files, but don't want to expose all the public (to the linker) symbols in each object file. That is there are occations when a symbol is public in one of the object file for the only purpose to make it available to another object file in the same library.
Note that public
and private
accessors are not quite the same as private symbols in an object file. While private symbol in an object (fx created with global static
qualifier) file means that the symbol is not visible at all outside the object file, while private
accessors just mean that the symbol can't be accessed from outside the class (it still can make it into the object file as public since it could be accessed from the same class within another translation unit).
If you want to export all public symbols in the object files there may be a mechanism to do that without having to resort to explicitely mark each symbol as dllexport
- you have to check the documentation, apparently there's a way in microsoft for that.
And for the story about dllimport
you can read it here: Why/when is __declspec( dllimport ) not needed?