Solved

utilizing mfc class functionality

Posted on 2004-09-20
5
192 Views
Last Modified: 2013-11-20
hi all,
  Is it right that you can use the functionality of a class even though you don't instantiate that class and create an object?? For example, when you use AfxGetApp() function, it returns a pointer to a CWinApp class.  But if I never instantiated an object of that class, then what is it pointing to?

0
Comment
Question by:struggling_coder_3203
[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 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 12107372
About specific case of AfxGetApp() function: In every MFC application generated with appWizard, a CWinApp class derived object is created. It is called 'theApp', so AfxGetApp() returns a pointer to this class.
Also, static class functions, can be used without instantiating a class. For example

CWnd::FromHandle(HWND hWnd) is a static function that returns a pointer to a window based in its handle number.

CTime::GetCurrentTime() returns an CTime object corresponding to current date/time, it is similar to time(NULL) function. You don't need a previous object to call it.
0
 

Author Comment

by:struggling_coder_3203
ID: 12107487
so an object of a class must be instantiated in order to use the functionality???
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12107507
If you plan to use non-static functions, definitively YES.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 12109933
Functions can be defined as static and, by default, non-static.
Roughly a static function does not require any initialisation that would be performed in the constructor of the class.  This allows one to call a static function of a class without it being instantated.
Give it a try - create a new class yourself and do something like in the header
static int MyStaticFn() {return 2;};
now in your prog do
int x = CMyTestClass::MyStaticFn();
x now has the value 2.
0
 

Author Comment

by:struggling_coder_3203
ID: 12128732
so, are all mfc classes static in a wizard generated application????????????????????????????
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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 video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

695 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