Solved

Function Prototype

Posted on 2000-05-11
5
563 Views
Last Modified: 2013-11-20
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)
{...}
0
Comment
Question by:teeyushg
  • 2
  • 2
5 Comments
 
LVL 1

Expert Comment

by:jacobkristensen
ID: 2799191
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
0
 
LVL 4

Accepted Solution

by:
captainkirk earned 50 total points
ID: 2800627
A1a) The return type of InitInstance() is a BOOL (a boolean true/false value)

A1b) If you declare a prototype without a return type, the compiler will assume the return type is an "int".

A2) The prototypes are necessary because, as you have noted, they are overridden. Now you have a class DERIVED from CWinApp, say, CWinYourApp. You will need a CWinYourApp::InitInstance() function.
0
 

Author Comment

by:teeyushg
ID: 2801160
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!
0
 

Author Comment

by:teeyushg
ID: 2801166
Thanks to Jacob too! :)
0
 
LVL 4

Expert Comment

by:captainkirk
ID: 2801216
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"
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need Help INsttalling wget on Mavericks OS X 3 90
no14 challenge 14 62
Use of condition with 'serial' in ansible 2 49
x-bar in Google Sheets 2 25
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now