Jump to content
  • Advertisement
Sign in to follow this  
JasonHise

initialization

This topic is 5044 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
Advertisement
I think the declaration of fiacu is interpreted like a fonction declaration. Try this :

static ForceInitAndCleanup fiacu;

Share this post


Link to post
Share on other sites
huh, indeed it was. Works great now, thanks! Although I would like to know... if that was valid function syntax, what does it mean for a function to be static?

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!