Link to home
Start Free TrialLog in
Avatar of struggling_coder_3203
struggling_coder_3203

asked on

CWinApp

Hi all,
  Is it safe to derive two different classes from CWinApp in the same application???

Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

What for? It is not usual and highly non-recommended by Microsoft documentation.
You can derive another CWinThread object instead, or you can create 2 different application projects with some inter-communication.
Avatar of struggling_coder_3203
struggling_coder_3203

ASKER

I know that the circumstance would be very odd in which to do so, but lets just say that I cannot get a pointer to the one and only app, and I want to use the functionality of the CWinApp class. Could it cause many problems???

p
Avatar of AndyAinscow
AfxGetApp returns a pointer to the CWinApp - you then cast it to your app to get at any functions/variables in your app's class.
eg.

CMyApp* pApp = (CMyApp*)AfxGetApp();
pApp->DoMyFunction();


Your app has only one running instance, why could you require two different CWinApp based classes.  If you want common functionality between two different app you could do

class CMyAppBase : public CWinApp
{
void SomeCommonFunction();
....

class CMyAppThis : public CMyAppBase
class CMyAppTheOther : public CMyAppBase

then you just have the files for CMyAppBase shared between two projects.
The CWinApp class is the base class from which you derive a Windows application object. An application object provides member functions for initializing your application (and each instance of it) and for running the application.

Each application that uses the Microsoft Foundation classes can only contain one object derived from CWinApp. This object is constructed when other C++ global objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the Microsoft Foundation Class Library. Declare your derived CWinApp object at the global level.

When you derive an application class from CWinApp, override the InitInstance member function to create your application's main window object.

CWinApp is designed to make sure that only application object is present in a given instance. CWinApp and its descendants are called Singleton Classes.


You can derive any number of classes from CWinApp class. The problem is that you can create only one CWinApp or CWinApp-derived variable. Once you try to create another CWinApp instance, you get large number of assertions, exceptions etc. when program is initialized.

tsaditya - CWinApp is not defined as singleton  by classic C++ way (private constructor and CreateInstance function). We can create MFC application and add second instance of CWinApp class, it is successfully compiled and linked:

CTestApp theApp;
CTestApp theApp1;

However, such program cannot be executed successfully, so we can think that CWinApp is actually singleton.

struggling_coder_3203 - possibly you need to describe your initial problem. The way you are trying to solve it is very difficult, I beleive there is another way.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial