[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to get the runtime class?

Posted on 2008-11-04
3
Medium Priority
?
874 Views
Last Modified: 2013-12-14
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

0
Comment
Question by:markgooding
3 Comments
 
LVL 45

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 22877401
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
 
LVL 19

Assisted Solution

by:alb66
alb66 earned 500 total points
ID: 22877426
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
 
LVL 4

Author Comment

by:markgooding
ID: 22877491
I'd always wondered what virtual did. Never got around to looking that up. Thanks!
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question