Solved

Function Prototype

Posted on 2000-05-11
5
570 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

630 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