How to initialize static out-parameters

144 views Asked by At

Sorry if this is a basic question. Basically, I'm implementing a class with a bunch of static (and unchanging) resources that will be shared by all instances. The static resources need to be initialized once by being passed as out parameters to various functions. I wasn't sure exactly how to do this elegantly. At the moment, what I do is put all the necessary function calls within a lambda, or a private member function, and then I call that function in a static manner within the constructor, like so:

HRESULT MyClass::FinalConstruct()
{
    static HRESULT hr = []() {

        HRESULT hr;

        // Call necessary initializing functions,
        // passing static variables as out-parameters. For example:
        // (s_pUIA and s_pUIATreeWalker are static smart pointers that
        // need to be initialized)

        hr = CoCreateInstance(  __uuidof(CUIAutomation),
                                NULL,
                                CLSCTX_INPROC_SERVER,
                                IID_PPV_ARGS(&s_pUIA)  );
        if (FAILED(hr))
        {
            ATLTRACE("\nFailed to instantiate IUIAutomation object.\n");
            return hr;
        }

        hr = s_pUIA->get_ControlViewWalker(&s_pUIATreeWalker);
        if (FAILED(hr))
        {
            ATLTRACE("\nFailed to get IUIAutomation tree walker.\n");
            return hr;
        }

        // ...

        return hr;

    }();

    return hr;
}

I wasn't sure if there was a more acceptable way of doing this. Thank you very much for any input.

0

There are 0 answers