Solved

utilizing mfc class functionality

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
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 video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

24 Experts available now in Live!

Get 1:1 Help Now