Solved

utilizing mfc class functionality

Posted on 2004-09-20
5
181 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
  • 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

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

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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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.

863 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

27 Experts available now in Live!

Get 1:1 Help Now