How to get the runtime class?

I've created a Doc/View architecture application using MFC. I have a number of view class types derived from a parent view class, which itself is derived from CView. During the program execution I get the currently active view using line 1 below.  I want to then call a function on that view, but at the child class level not the parent, or CView, level. Is there a way to do that without repeating lines 3-5 below for each class?
CView* testview = ((CFrameWnd*) AfxGetApp()->m_pMainWnd))->GetActiveView();
 
if(testview->IsKindOf(RUNTIME_CLASS(CMyChildView))){
 ((CMyChildView*)testview)->DoSomething();
}

Open in new window

LVL 4
markgoodingAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
Use a virtual function.

CMyBaseClass..
{
virtual void DoSomething() { ASSERT(FALSE); };
};




COneClass : public CBaseClass
{
virtual void DoSomething() ;
};

CTwoClass : public CBaseClass
 {
 virtual void DoSomething() ;
 };


Then elsewhere
pView->DoSomething();   //compiler will 'know' which DoSomething to call.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
alb66Commented:
Derivae all the views from a common base class (derived from CView). Add a virtual method in that class and override it in the derived classes.


class CBaseView : public CView 
{
   virtual void DoSomething() {}
}
 
class CMyChildView1: public CBaseView 
{
  virtual void DoSomething();
}
class CMyChildView2 public CBaseView 
{
  virtual void DoSomething();
}
 
 
CView* testview = ((CFrameWnd*) AfxGetApp()->m_pMainWnd))->GetActiveView();
 
if(testview->IsKindOf(RUNTIME_CLASS(CBaseView ))){
 ((CBaseView *)testview)->DoSomething();
}

Open in new window

0
markgoodingAuthor Commented:
I'd always wondered what virtual did. Never got around to looking that up. Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.