Thursday, November 14, 2019

Extern "C" function internally uses C++ class




Is this legal to do? I want to export a C function, but internally that function will use a C++ class.




extern "C" BOOL /*BOOL==int*/ Func()
{
return someclass::getinstance()->Func(); // this is just bool tho
}

Answer



This is perfectly legitimate. The purpose of extern "C" is to prevent Func() from getting its name mangled (decorated with type information) so that a C module can link to it using its plain name. C++ mangles names so that functions with the same name but different parameter lists can be resolved (function overloading).


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...