This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Initialization methods in library..


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



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]