Link to home
Start Free TrialLog in
Avatar of teeyushg
teeyushg

asked on

Function Prototype

I am a newbie trying to learn MFC through self-study.  Came across this section of code from the MSDN library, on which I have some questions.  Would appreciate help.  Thanks.

Referring to the code appended:

Question 1:
What is the return type of InitInstance() and InitApplication()?  When is it legal to declare the prototype without a return type?

Question 2:
Why must the prototypes of these two functions be declared?  If the code had #included <afxwin.h> (and therefore the two functions would have been implemented as member functions of MFC class WinApp), would there be a need to declare the prototypes of these two functions, or could I just have used CWinApp::InitInstance() straight without the prototype declaration?  Is the prototype declarations made necessary because InitInstance() and InitApplication() are overridden?

==========================

#include <windows.h>

// Global variable
HINSTANCE hinst;

// Function prototypes.
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
InitApplication(HINSTANCE);     //<-- ?
InitInstance(HINSTANCE, int);   //<-- ?
LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);

// Application entry point.
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{...}
Avatar of jacobkristensen
jacobkristensen

The declarations for InitInstance is as follows:

virtual BOOL InitInstance();

The CWinApp::InitApplication member function is obsolete in MFC. An initialization that you would have done in InitApplication should be moved to InitInstance!

Regarding your second question, as you can see InitInstance() is a virtual function which means that it must be overridden, therefore you must also declare it in your header file.

Jacob
ASKER CERTIFIED SOLUTION
Avatar of captainkirk
captainkirk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of teeyushg

ASKER

Would appreciate if you could explain a little further:

I copied the code verbatim from MSDN online.  The only #include file is Windows.h

1.  I can't seem to find any declaration of the "virtual BOOL InitInstance()" in Windows.h or in the files #included in it.  Where is it declared, in the case of this code? (I find it declared in afxwin.h though, but afxwin.h is not #included in this piece of code)

2.  As you mentioned, return type for InitInstance() is BOOL; and also that where return type not stated, type is INT.  So what is the reason for the omission of the return type from the function prototype?

Thank you!
Thanks to Jacob too! :)
1) the prototype for InitInstance() is indeed in AfxWin.h... someone correct me if I'm wrong, but I believe the sample you found relates to older Windows versions... now the framework uses the AfxWin.h prototype and the implementation of the CWinApp::InitInstance() function is in appcore.cpp


2) if indeed the sample is from an older version of Windows, the return type may have been "int" and has since changed to "BOOL"