Sign in to follow this  

initialization

This topic is 4860 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was writing an OOP wrapper for a C-style engine, and the engine required an Init() call at the beginning of the program and a CleanUp() call at the end. My method of making sure these got called was to put the following code in the source file of my wrapper class:
struct ForceInitAndCleanup
{
    ForceInitAndCleanup (  )
    {
        Init (  );
    }

    ~ ForceInitAndCleanup (  )
    {
        CleanUp (  );
    }
};

static ForceInitAndCleanup fiacu (  );
However, for some strange reason, fiacu's ctor and dtor never got called. I added a dummy int to the ctor, and it worked fine. Is C++ really allowed to optimize away construction of a static object if no parameters are passed, or is that just a flaw in my compiler?

Share this post


Link to post
Share on other sites
Static on a function means that you can't access that function from outside of that translation unit. Say I declared the function doSomething in doSomething.c as static. If I tried to access it from anywhere other than doSomething.c I would get a linker error saying doSomething wasn't defined.

Share this post


Link to post
Share on other sites
Quote:
Original post by bytecoder
Static on a function means that you can't access that function from outside of that translation unit. Say I declared the function doSomething in doSomething.c as static. If I tried to access it from anywhere other than doSomething.c I would get a linker error saying doSomething wasn't defined.


Thats valid for C, but its deprecated (that means your compiler may or may not support this) in C++ to achieve the same effect using standard C++ code you would use anonymous namespaces.

Share this post


Link to post
Share on other sites

This topic is 4860 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this