Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about Casting Pointers - need Explanation!

Status
Not open for further replies.

pghTech

Technical User
Jul 19, 2006
37
US
I am reading a book that is helping me develope a application to access a MS Access DB through ODBC with MFC.

They walk you through manually adding a class derived from CRecordView. In the process, they have you add a *second* derived class from CRecordView to you program (the first being the original derived class from CRecordView when you went through the *New project* wizard).

They state the following step that needs to be done in the second derived class from CRecordView to cast the pointer to to (GetDocument()) member function of the Document class and list the following reason - I was hoping someone can break down their reasoning for having to cast the pointer, as their explanation isn't quite clear to me.

"The version of the COrderSet Class that was implemented by the Class Wizard doesn't override the GetDocument() member because it isn't associated with the document class initially. As a result, you must cast the pointer from the base class GetDocument() member to a point to a CDBSampleDoc object. "

This is the code the book just had me add to the second derived View class from CRecordView
Code:
void COrderView::OnInitialUpdate()
{
  BeginWaitCursor();
  [highlight]CDBSampleDoc* pDoc = static_cast<CDBSampleDoc*>(GetDocument());  [/highlight]
   m_pSet = &pDoc->m_OrderSet;           // Get a pointer to the recordset

   // Use the DB that is open for products recordset
   m_pSet->m_pDatabase = pDoc->m_DBSampleSet.m_pDatabase;

   // Set the current product ID as parameter
   m_pSet->m_ProductIDparam = pDoc->m_DBSampleSet.m_ProductID;

   // Set the filter as product ID field
   m_pSet->m_strFilter =
     "[ProductID] = ? AND [Orders].[OrderID] = [Order Details].[OrderID]";

   CRecordView::OnInitialUpdate();
   EndWaitCursor();
}

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top