Click to See Complete Forum and Search --> : Permanent pointers


TrimTrom
July 31st, 1999, 06:07 AM
Hello,
I am a beginner using VC++6 Prof and Windows 98.

I am writing an MDI application and am currently writing the first
document/view scenario, which is a splitter with treeview/listview.

One of my menu options is a function which I am handling in the
document. In this I need a pointer to the listview, and my problem is
that I need to know how to get this pointer. What I have done is to
declare a pointer (called AddrPointer) in the document class
declaration, and then in the listview I did this:

void CAddrList1::OnInitialUpdate()
{
CListView::OnInitialUpdate();

// TODO: Add your specialized code here and/or call the base class
CAddrDoc1* tempdoc = (CAddrDoc1*)GetDocument();
tempdoc->AddrPointer = this;
}



I made the pointer equal to "this" in the listview class. I like this
method because the pointer is available permanently when the
document/listview is open.

I would like to know if this is a good method, because I have frequently
seen warnings in MSDN that some pointers are only temporary etc. etc..
Is there a possibility that under some circumstances this pointer is not
accessible?

I would be grateful for advice on the subject, and code snippets showing
what is the correct method if what I have done above is wrong.

Thanks,
Paul Trimming
--

----------------------------------
Email: paul.trimming_nospam@dial.pipex.com
Surrey, England
----------------------------------

jteagle@home
July 31st, 1999, 08:01 AM
What you have done is perfectly safe. The temporary pointers talked about in the help are those generated from attempting to grab a window from another application which may not be written with MFC, and by wrapping an HWND using CWnd::FromHandle().

However, as you are asking about safety and perhaps about style, I would say the following:

1) You are setting a public member variable in another class (tempdoc->AddrPointer). The whole point of C++ and OO is to HIDE data members so that other classes can't alter them at will. Therefore, I would recommend providing a member function to set this pointer, and make the variable private.

2) Although it would probably never apply to your project, there is a possibility that the tree or list view might be destroyed and recreated (or another view created in its place). This would render the pointer invalid. Instead of the method you have chosen, consider using

---CMyListView *CMainFrame::GetListView(void)
{
CMyListView *pView ;

pView = (CMyListView *)m_wndSplitter.GetPane(0, 1); // Row, column.
}




---

as a means of getting the pointer, which will remain safe even if the views are changed.

Alvaro
July 31st, 1999, 02:53 PM
You're not the first developer to do something similar. I've seen other people's programs where the CDocument class needed a pointer to its views and the quickest and easiest way to do it is with public member pointers of the class.

Then you rely on each of the view classes to set their corresponding pointers after being created. What I do is set it right after it is created, instead of in OnInitialUpdate like you have it. So I provide a handler for WM_CREATE which creates an OnCreate method and there I set the pointer, sort of like this:

int CMyTreeView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CTreeView::OnCreate(lpCreateStruct) == -1)
return -1;

GetDocument()->m_pTreeView = this;
return 0;
}



I know from a pure OO standpoint this isn't recommended but if you know what you're doing, it's certainly the most efficient and easiest way to do things.

One thing I did notice is your lack of conventions for your variables. I recommend, if you're going to be doing extensive C++ programming, that you seriously consider using some sort of convention for naming your variables, classes, functions, etc. Believe me, it will make your life much easier when you (or others) go read your code at a later date.

Alvaro