creating an instance of a mfc derived class.
I need to create an object of a mfc derived CFormView class that's not in the doc/template (a second view class).
but it was generated with a protected ctor. Here's the code explanation with comments.
Code:
// I'm thinking all the normal classes of the Doc/View template are created
// starting with this code, but within the template code base.
CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(IDR_MAINFRAME,
RUNTIME_CLASS(CViewSwitchDoc), //<-expands to-> ((CRuntimeClass*)(&CViewSwitchDoc::classCViewSwitchDoc)),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CViewSwitchView));
//
// But I have generated "another view" using the "Add Class" Wizard, it's a derived class of
// mfc CFormView which I named ViewForm. However I'm having a problem creating an instance
// of it because of the generated protected ctor and pulls a compile error of not being able to access ctor.
// Below are the header and implementation files of this said ViewForm class.
// Anybody got any idea on how to create an object of this view ?
// Did I go about it all the wrong way since it's not in the doc/template group ?
//=================================================
// ViewForm.h file
#pragma once
// ViewForm form view
class ViewForm : public CFormView
{
DECLARE_DYNCREATE(ViewForm)
//public: static const CRuntimeClass classViewForm; // These 3 lines are expansion of
//virtual CRuntimeClass* GetRuntimeClass() const; // DECLARE_DYNCREATE(ViewForm)
//static CObject* __stdcall CreateObject();
protected:
ViewForm(); // generated by Add Class wizard with
//protected constructor used by dynamic creation
virtual ~ViewForm();
. . . . . other code not pertinant
. . . . .
};
//===============================================
// ViewForm.cpp : implementation file
#include "stdafx.h"
#include "ViewSwitch.h"
#include "ViewForm.h"
IMPLEMENT_DYNCREATE(ViewForm, CFormView)
// the below is expansion of IMPLEMENT_DYNCREATE above,
// CObject* __stdcall ViewForm::CreateObject() { return new ViewForm; } // this is where obj would be created.
// including the embedded IMPLEMENT_RUNTIMECLASS below,
//__declspec(selectany) const CRuntimeClass ViewForm::classViewForm =
// { "ViewForm", sizeof(class ViewForm), 0xFFFF, ViewForm::CreateObject,
// ((CRuntimeClass*)(&CFormView::classCFormView)), 0, 0 };
//CRuntimeClass* ViewForm::GetRuntimeClass() const { return ((CRuntimeClass*)(&ViewForm::classViewForm)); }
ViewForm::ViewForm()
: CFormView(ViewForm::IDD)
{ }
ViewForm::~ViewForm()
{ }
. . . . other non pertinant code
. . . .
}
Re: creating an instance of a mfc derived class.
Quote:
Originally Posted by
J_W
I need to create an object of a mfc derived CFormView class that's not in the doc/template (a second view class).
but it was generated with a protected ctor.
So make it public. You're allowed to alter the generated code, you know. ;)
Re: creating an instance of a mfc derived class.
Quote:
Originally Posted by
D_Drmmr
So make it public. You're allowed to alter the generated code, you know. ;)
Oh, ok, for some reason I thought that might foobar the MFC framework that made it protected. Wonder why the derived CFormView class has a protected ctor and the other derived classes don't. Maybe I should also create one with CFormView inside the doc/template and trace thru and see how it gets created.
Anyhow I'll try it uprotected and see if something gets infected. Thanks.
Re: creating an instance of a mfc derived class.
When all else fails, RTFM. That would be quicker, easier and more productive than posting here, and really should be done first. Don't confuse the forum with MSDN.
"Your derived class must supply its own constructor. In the constructor, invoke the constructor, CFormView::CFormView, with the resource name or ID as an argument as shown in the preceding class overview."
http://msdn.microsoft.com/en-us/libr...=vs.80%29.aspx
Re: creating an instance of a mfc derived class.
Quote:
Originally Posted by
J_W
Oh, ok, for some reason I thought that might foobar the MFC framework that made it protected. Wonder why the derived CFormView class has a protected ctor and the other derived classes don't. Maybe I should also create one with CFormView inside the doc/template and trace thru and see how it gets created.
I think the idea is (was) that you only instantiate the class using the CRuntimeClass interface. IIRC this is also how the doc template framework does it. However, the CRuntimeClass design is so hopelessly outdated that there is little point in using it in your own code.
Re: creating an instance of a mfc derived class.
Quote:
Originally Posted by
GCDEF
When all else fails, RTFM. That would be quicker, easier and more productive than posting here, and really should be done first. Don't confuse the forum with MSDN.
"Your derived class must supply its own constructor. In the constructor, invoke the constructor, CFormView::CFormView, with the resource name or ID as an argument as shown in the preceding class overview."
http://msdn.microsoft.com/en-us/libr...=vs.80%29.aspx
Ok point taken, will do. However many times on here I do get good input on here as to specifically where to RTFM, as your current post content (in addition to the RTFM) helps also. Thanks.
Quote:
Originally Posted by
D_Drmmr
I think the idea is (was) that you only instantiate the class using the CRuntimeClass interface. IIRC this is also how the doc template framework does it. However, the CRuntimeClass design is so hopelessly outdated that there is little point in using it in your own code.
Thank you, so then I would be just as well off (or better off if I do not need to reference an IsKindOf ..etc ) to just create the object straight out with my own new MyClassName
-----------------------------------------------
Anyhow just for curiousity I did this before unprotected the derived ctor as D_Drmmr suggested.
I created a separate project with the derived formview inside the Doc/Template (no second view).
As one would figure the object creation is originated from the Doc/Template code framework, tracing shows the template creation is first started and then eventually all classes within the template are then instantiated.
Code:
//from the IMPLEMENT_DYNCREATE expansion of CF_TraceView
CObject* __stdcall CF_TraceView::CreateObject()
{ return new CF_TraceView; }
// this calls into
void* PASCAL CObject::operator new(size_t nSize, LPCSTR lpszFileName,
int nLine)
{return ::operator new(nSize, _AFX_CLIENT_BLOCK, lpszFileName, nLine);
}
// and eventually down into malloc etc.
-------
// And when the ctor of the base class (the mfc CFormView) (from which
// my derived FormView) is where protected stuff is goin on.
// base class header file ctor
protected: // must derive your own class
CFormView(LPCTSTR lpszTemplateName);
CFormView(UINT nIDTemplate);
// base class cpp file ctor
CFormView::CFormView(UINT nIDTemplate)
{
ASSERT_VALID_IDR(nIDTemplate);
m_lpszTemplateName = MAKEINTRESOURCE(nIDTemplate);
m_pCreateContext = NULL;
m_hWndFocus = NULL; // focus window is unknown
#ifndef _AFX_NO_OCC_SUPPORT
m_pOccDialogInfo = NULL;
#endif
EnableActiveAccessibility();
}
So anyhow I went back to my original project where my derived formview was outside the doc/template as a second view and just put the derived ctor public instead of the VS generated protected. It compiled ok and "appears" to run ok, with zero exit codes and the following printouts from the ctor, dtors.
(edit original errant paste)
From the CViewSwitchApp ctor
Just entering App InitInstance
From the ViewForm ctor
From the CMainFrame ctor
From the CViewSwitchView ctor
From the ViewForm dtor
From the CViewSwitchView dtor
From the CMainFrame dtor
-----
(the default dtor for the app class was not in code)
Note: the 2nd viewfrom class object was embedded as a member of the app class
and instanciated right after the doc/template comletion in the app's
InitInstance function. (show below)
AddDocTemplate(pDocTemplate);
ViewForm My_2nd_ViewObj;
====
Sorry for any redundance, you guys have been a great help to me. This is a great forum.