Sign in to follow this  
yoshscout

Factories and static linking

Recommended Posts

yoshscout    116
Im running into a problem in my engine design that is not uncommon as I have seen from many threads here in gamedev. I want my code to be extremely reusable so I have put a lot of planning into the structure. It's mainly a C++ problem I believe. I have found a temporary work around but this will not solve my problem in the end as it will hamper my reusability. If i move the following line from its real home in the object file in the engine's static build to right before main on the server build, all goes well. std::map<int, NetMsgMaker*> NetMsgMaker::Registry; This static object is a std::map. NetMsgMaker is a class, therefore it is a map of integers to class pointers. This std::map has no _Root, which leads me to believe it is not initialized when it is in the object file. However, it initializes fine when placed before main on the server build. Is there any way I can make this static class variable initialize in its home? It really belongs in the library's code not in the application's code. Ive seen numerous threads on this problem of having a static class member in library not initializing and additionally constructors not getting called on said items. It really throws the whole factories implementation off.

Share this post


Link to post
Share on other sites
Um, I have static variables in library modules that fire up just fine. The only potential problem is that the order of initialization isn't at all guaranteed. If I need a guarantee as to when a static is initialized, then I wrap it in a function, and use the function call to access it.

What compiler are you using?

Share this post


Link to post
Share on other sites
ApochPiQ    23061
More importantly, when are you checking the map to see if it has been initialized? Because static order of construction is not well-defined, it's possible that you're looking at the map from some other static-construction code (e.g. the constructor of a global object, for example).

If that is the case, you'll need to use some kind of deferred-construction method, or restructure your code. This page should prove helpful in arriving at a solution.

Share this post


Link to post
Share on other sites

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