Skip to main content
GameDev.net gamedev.net
🔒 Locked

Question about Casting Pointers - need Explanation!

Started by pghTech Sep 1, 2006 at 9:36 AM 2 replies 650+ views
Original Post
pghTech
pghTech
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

void COrderView::OnInitialUpdate()
{
  BeginWaitCursor();
  CDBSampleDoc* pDoc = static_cast<CDBSampleDoc*>(GetDocument());  
   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
pghTech
pghTech
None?
Aardvajk
Aardvajk
What type does GetDocument() return?

This looks like a sort of hack that relies on the fact that whatever type GetDocument returns has within it, at the same offset as the m_OrderSet member of CDBSampleDoc relative to the start of CDBSampleDoc, an object of the same type as whatever m_pSet is.

I don't really understand their explanation that you posted though.
Endurion
Endurion
I'm not sure if that applies here but i remember having a similar "problem" because of multiple virtual inheritance. Basically meaning that the casting to the base class pointer actually changed the address value. Therefore you had to cast to the base class before so the address was set correctly.
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.