Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3358438
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:45:10+00:00 2026-05-18T02:45:10+00:00

I have a class that only has static members. I would like to register

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:45:10+00:00Added an answer on May 18, 2026 at 2:45 am

    It is possible for a compiler to use different calling conventions for C and C++ code; however, in practice, this almost never occurs.

    If you just want it to work and don’t care about supporting obscure compilers, don’t bother with extern "C". It’s not necessary in any widely-used compiler.

    If you want to be absolutely pedantic, or need to support a pedantic compiler, write a wrapper:

    extern "C" static void ExampleVerifyClean()
    {
      Example::VerifyClean();
    }
    
    void Example::Initialize()
    {
        dirtLevel = 0;
        atexit(&ExampleVerifyClean);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using a Java class library that is in many ways incomplete: there are
one of my favorite annoyance when coding in C++ is declaring some static variable
I'm writing a custom DAL (VB.NET) for an ordering system project. I'd like to
I have an ASP.NET project which is a front-end to a database. In addition
I want to ask a question about how you would approach a simple object-oriented
I just wanted to share this Query class and get your thoughts on it.
This is a memory allocation issue that I've never really understood. void unleashMonkeyFish() {
I have a web-application which uses hibernate and for some reason every thread (httprequest
Today I've stepped into a problem I thought was so common I got myself
This question is related to How to make consistent dll binaries across VS versions

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.