I have taken over the development of some GUI code in C++, and I'm having trouble accomplishing what I want. The GUI has a class derived from CPropertySheet that has 5 property page classes all derived from CPropertyPage. The CPropertySheet class uses the pages in its functions. One function (SetActivePage) calls the base class SetActivePage function, and then it asks the page to load it's data. The way it is written, it checks what page is selected (by the index passed in to the SetActivePage function when the user switches pages. If the page is 1, then it calls the load data function for the class pertaining to page 1. etc. for each page. This seems like a lot of code. I wanted to use virtual functions, and in the CPropertySheet function, I wanted to replace all those conditionals with just two lines of code:
CMyPage * myPage = GetActivePage(); //get the right page
myPage->LoadData() //where loaddata is a virtual function
//for all the pages derived from MyPage
//which is derived from CPropertyPage
So I created a class CMyPage (Name changed to protect project confidentiality). CMyPage is derived from CPropertyPage. CMyPage is now the base class of the 5 property page classes that used to be derived from CPropertyPage. I wrote a virtual LoadData() member function on each of the derived classes and wrote a pure virtual function LoadData on CMyPage class. Ideally, the above code would work if GetActivePage returned a pointer to the actual class or to the CMyPage class.
However, GetActivePage() (which is a member function of CPropertySheet) returns a pointer to CPropertyPage, which cannot be converted to a pointer of the derived class CMyPage. I somehow need to be able to get a pointer to the derived class or the class of the actual object.
Does anyone have a better architecture solution or a way to make this solution work?