Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

using pointers to access virtual functions in derived classes

using pointers to access virtual functions in derived classes

using pointers to access virtual functions in derived classes

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?


RE: using pointers to access virtual functions in derived classes

I had that problem.  I can't remember the exact details but here is the gist.

The way I got around it was by creating a vector of CMyPage pointers.  As the pages are created, they are added to the vector.  Instead of using GetActivePage, get the index of the page.  I can't remember how this was done.

Anyway, use the index into the vector and you'll get the correct class + virtual function every time.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close