I was presented with a C++ DLL source code that uses extern "C":
extern "C"
{
class Something
{
public:
__declspec(dllexport) Something();
__declspec(dllexport) virtual ~Something();
__declspec(dllexport) bool function_one(const char * some_text);
static __declspec(dllexport) char * get_version();
private:
unsigned int m_data;
};
}
The DLL is being called by a C++ program. FYI, using Visual Studio 2017 on Windows 7 platform.
Questions *(all related to the extern "C" and class):
- Since
classis not C language, will this be equivalent to astruct? - Are constructors valid?
- Are virtual destructors valid (since C doesn't have
virtual)? - How is the
boolhandled? - How is
statictreated inside theextern "C"for the class? - How is
privatedata handled inside theextern "C"block? - How is
noexcepthandled in anextern "C"block for the constructor?
The Visual Studio 2017 compiler is not generating any errors or warnings with the above code.
The VS2017 code analyzer only generates a warning for the constructor:
C26439 This kind of function may not throw. Declare it 'noexcept' (f.6).
Research:
The questions on StackOverflow related to this issue mention that the "extern "C"has the effect of resolving name mangling. However, they don't address the issues ofvirtual,bool`, private data, and etc. as I listed above.
Also, many DLLs related answers recommend not using non-POD structures because the layout may change between compilers (including same versions of compilers); so for example, character arrays are preferred over std::string.