I was previously using a CSplitterWnd
in a MFC application, using it's CreateView
function. Everything was working fine but now I would like to pass a parameter to the constructor of my views, so I cannot use MFC dynamic object creation (DECLARE_DYNCREATE
and IMPLEMENT_DYNCREATE
) because they require an empty constructor.
After searching a little on the internet I found an exemple that looks like this:
m_wndSplitter.CreateView(0,0,RUNTIME_CLASS(CMyView), CSize(0,0), pContext);
m_wndSplitter.CreateView(0,1,RUNTIME_CLASS(CMyView), CSize(0,0), pContext);
m_pView0=(CMyView *)m_wndSplitter.GetPane(0,0);
m_pView1=(CMyView *)m_wndSplitter.GetPane(0,1);
This could be a workaround (i.e.: create a new function in CMyView
letting me specify what I want) but this would be ugly and error prone. Anyone know if there is another way I could do this?
Edit: Adding more details after ee's answer:
Your right that the initialize method would work but this force me to remember to call that initialize method, but like you pointed out I will probably not create these views many times so that should be ok. Another thing I would maybe like is to manage the lifetime of the view myself so again this is not possible using CreateView.
Thanks
After checking Javier De Pedro's answer I though I could override the creation function so I did (semi-pseudo-code):
Now this work but there is the problem that it will destroy my class when closing and I said I would maybe want to track memory myself so I overloaded PostNcDestroy in CMyView to do nothing instead of calling delete this:
Now it should prevent it from getting deleted but now it crash when exiting so I overriden CMyFrame::OnClose like this:
Now theorically I should be able to keep the myView pointer elsewhere as long as I delete it before the document exit.
Thanks for your help guys.