Solved

Function Prototype

Posted on 2000-05-11
5
561 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

21 Experts available now in Live!

Get 1:1 Help Now