Factories and static linking

Started by
3 comments, last by yoshscout 17 years ago
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.
Advertisement
even more confusing a frustrating is that there has to be a way to do this because it works in the client project it doesnt in the server
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?
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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

VC++

you might be right about calling from another constructor that might be it

This topic is closed to new replies.

Advertisement