Initialization methods in library..

Eljay Love-Jensen eljay@adobe.com
Tue Aug 26 12:43:00 GMT 2003


Hi Ajay,

What you are running into is an "order of construction" problem.

The general strategy to fix the problem is to make an accessor routine that returns the static variable.

Example:
class g_Master
{
public:
    static CSmSimpleObj& getCSmSimpleObj();
}

CSmSimpleObj& g_Master::getCSmSimpleObj {
    static CSmSimpleObj CacheCount;
    static bool doInit = true;
    if(doInit) {
        CacheCount->Setvalue(1);
        doInit = false;
    }
    return CacheCount;
}

If you REALLY want to have the _initfunc in the other library do the Setvalue(1), you'll need something like this:

CSmSimpleObj& g_Master::getCSmSimpleObj {
    static CSmSimpleObj CacheCount;
    static bool doInit = true;
    if(doInit) {
        _initfunc();
        doInit = false;
    }
    return CacheCount;
}

HTH,
--Eljay




More information about the Gcc-help mailing list