Calling C++ function pointers from C libraries
<p>I have a class that only has static members.</p>

<p>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.</p>

<p>The <a href="http://yosefk.com/c++fqa/function.html#fqa-33.2" rel="nofollow">C++ FQA</a> says that i must specify extern "C" for the function i want to register this way, like in the following example.</p>

<pre><code>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(&amp;VerifyClean);
}
</code></pre>

<p>Do i really have to use extern "C"?</p>

<p>Does the answer change if i replace "atexit" with a non-library function (implemented in plain C)?</p>

<p>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.</p>
