Using virtual-C++ vs MFc

Discussion in 'MFC' started by moihere, Jul 21, 2006.

  1. moihere

    moihere New Member

    Joined:
    Jul 18, 2006
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    I am stuck with a doubt! I have been working with MFC based applications for sometime. Decided to read up some theory to strengthen my fundamentals. Naturally, I was reading MFC concepts as well as C++ basics. What struck me was the way we use VIRTUAL in both.

    In C++, there goes a standard way to use virtual functions.
    - There is a Base class B having a function func() and its derived class D also uses/implements the function func(). If a function creates objects of both B and D and calls this func() through a pointer to B, the base class version of this function will be called UNLESS we declare func() as virtual .right??? So if we want to call func() from D's implemetation, we declare func() as virtual in Base class B , pass a reference of D's object to base class B's pointer and then call func() thru this pointer. I guess i got this right so far???

    Now, how do we go about it in MFC(which is also supposed to be C++ at the end of the day)? From what I have used so far..i use virtual funcs like this.

    Class CDialog has 3 virtual functions. Lets take OnInitDialog() as the example.
    Code:
    class CDialog
    {
     -----
     virtual BOOL OnInitDialog();
    -----
    }
    In my application, i mostly derive my own class from CDialog,
    Code:
    eg class CMyDlg : public CDialog
         {
           protected:
    	virtual BOOL OnInitDialog();
           }
    I override the OnInitDialog() function to suit my initialization needs.
    Then, how do i call OnInitDialog()??
    Answer: I do it THROUGH DoModal().
    Code:
    eg: OnBtOpenDlg() 
        {
           CMyDlg myDLg;
           if(myDlg.DoModal()==ID_OK)
                { --------}
        }
    This takes care of calling the derived class version of OnInitDialog() despite the fact that OnInitDialog is a virtual function inthe base class. We didn't assign any reference of myDlg to a base class pointer or anything?????????????????????????? :confused:

    Am i missing something or are they implemented differently in C++ and MFC programming????????
     
    Last edited by a moderator: Jul 21, 2006
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Thats when you need to call the derived class func from the base class but when you want to just call the function using the typecasted pointer its not needed.

    In MFC the case is the pointer is typecasted and so when the base class member is virtual correct OnInitDialog is called. Here is the code from MFC.

    Code:
    BOOL CALLBACK AfxDlgProc(HWND hWnd, UINT message, WPARAM, LPARAM)
    {
    	if (message == WM_INITDIALOG)
    	{
    		// special case for WM_INITDIALOG
    		CDialog* pDlg = DYNAMIC_DOWNCAST(CDialog, CWnd::FromHandlePermanent(hWnd));
    		if (pDlg != NULL)
    			return pDlg->OnInitDialog();
    		else
    			return 1;
    	}
    	return 0;
    }
    
    pDlg is type casted to your dialog instance and so your OnInitDialog is called.
     
  3. moihere

    moihere New Member

    Joined:
    Jul 18, 2006
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    So would the same logic be applied to all virtual functions overridden in MFC?(functions of CObject, of CWinApp, InitInstance etc.) Is it correct to say that most of these virtual functions which are overridden are downcasted/typecasted and the correct version of the function is called??
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Yes.
     
  5. moihere

    moihere New Member

    Joined:
    Jul 18, 2006
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Thanks!! That clears a big missing link !
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    My pleasure
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice