I have a class that only has static members.
I would like to register one of its member functions (VerifyClean in the code below) to be called at exit, using the "atexit" library function.
The C++ FQA says that i must specify extern "C" for the function i want to register this way, like in the following example.
class Example { public: static void Initialize(); static void DoDirtyStuff {++dirtLevel;} static void CleanUpStuff {--dirtLevel;} private: static void VerifyClean(); // DOESN'T COMPILE: extern "C" static void VerifyClean(); static int dirtLevel; } int Example::dirtLevel; extern "C" void Example::VerifyClean() // DO I NEED extern "C" HERE? { assert(dirtLevel == 0); } void Example::Initialize() { dirtLevel = 0; atexit(&VerifyClean); } Do i really have to use extern "C"?
Does the answer change if i replace "atexit" with a non-library function (implemented in plain C)?
If the function VerifyClean were public and i decided to call it directly from C++ code, would i get link errors or runtime crashes? I ask this because the declaration doesn't mention extern "C" at all, so regular C++ code might handle the function call incorrectly. This works OK on my MS Visual Studio 2005 system.