0

I saw this code:

HRESULT Helper::LoadHelper(void)
{
    HRESULT LaunchUserProcess(char* fileName);   //This is the line in question
    if (FAILED(LaunchUserProcess("helper.exe")))
    {
        return E_FAIL;    // Failed to launch helper
    }
    return S_OK;
}

Question is, what does this do:

HRESULT LaunchUserProcess(char* fileName);

Looks like a function prototype to me. But why is it in the middle of a method?? Also, there is another file that actually contains its implementation:

HRESULT LaunchUserProcess(char* fileName)
{
   //implementation here
}

Can anyone enlighten me please? Thank you!

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
kzaiwo
  • 1,558
  • 1
  • 16
  • 45
  • It's perfectly legal for a function prototype to be inside a method. Maybe not good style (why would you want to limit the scope of a function prototype?) but legal. – john Jul 23 '19 at 06:20
  • @NPE I wouldn't way this is exactly a duplicate of the other question because, in this case, it might be "required" to inlcude the function prototype before using it, whereas in the other question it is not required at all and appears to be pointlessly added... – code_fodder Jul 23 '19 at 06:51
  • 1
    @keechan, its probably there because the function prototype is not in any header included by the file that you are looking at. So if you tried to compile it without declaring the function-prototype the compiler would not know about this function. So by delaring it, the compiler knows about its existance and can compile. It is then up to the linker to deal with "finding" the function body/address. Usually this function declaration would be in a header and you would just include that... reasons to do this might be: so you don't need the entire header file or just want to reduce the scope. – code_fodder Jul 23 '19 at 06:55
  • Thanks @code_fodder! This clears it up for me. Turns out there was a header file but it was not included for some reason. And that the prototype from inside this method was used instead, as you said. Thanks everyone for your help! Much appreciated. – kzaiwo Jul 23 '19 at 07:17

0 Answers0