?
Solved

utilizing mfc class functionality

Posted on 2004-09-20
5
Medium Priority
?
196 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 1500 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

Technology Partners: 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: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month10 days, 20 hours left to enroll

770 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