Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. MFC VC++

MFC VC++

Scheduled Pinned Locked Moved C / C++ / MFC
c++
14 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Victor Nijegorodov

    What you see here is a member initialization list It is (almost) equivalent to initialization within a ctor like:

    MyDlg::MyDlg(CWnd* pParent /*=NULL*/)
    : CDialogEx(IDD_DLG, pParent)
    {
    m_Name = _T("");
    m_Status = _T("");
    m_Comments = _T("");
    }

    M Offline
    M Offline
    Member_14575556
    wrote on last edited by
    #3

    Thank you. That clear my doubt :) If you don't mind could you explain this line too:

    CHeaderCtrl* pHeader = (CHeaderCtrl*)m_ListControl.GetDlgItem(0);

    V 1 Reply Last reply
    0
    • M Member_14575556

      Hello Everyone, I have some doubt regarding below code snippet.

      MyDlg::MyDlg(CWnd* pParent /*=NULL*/)
      : CDialogEx(IDD_DLG, pParent)
      , m_Name(_T(""))
      , m_Status(_T(""))
      , m_Comments(_T(""))
      {

      }

      Please explain why m_Name(_T("")),m_Status(_T("")),m_Comments(_T("")) is there. Thank you. :) I'm new to MFC VC++.

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #4

      m_Name

      is an instance of the CStringT class. The

      m_Name(_T(""))

      initializes it (see Constructors and member initializer lists - cppreference.com[^] with the empty string (of TCHARS, see Generic-Text Mappings in tchar.h | Microsoft Docs[^]) literatal. The same happens with the other strings instance in the member initializer list.

      In testa che avete, signor di Ceprano?

      M 1 Reply Last reply
      0
      • CPalliniC CPallini

        m_Name

        is an instance of the CStringT class. The

        m_Name(_T(""))

        initializes it (see Constructors and member initializer lists - cppreference.com[^] with the empty string (of TCHARS, see Generic-Text Mappings in tchar.h | Microsoft Docs[^]) literatal. The same happens with the other strings instance in the member initializer list.

        M Offline
        M Offline
        Member_14575556
        wrote on last edited by
        #5

        Thank you. :)

        BOOL CMyDlg::OnInitDialog()
        {
        CDialogEx::OnInitDialog();
        ..........
        ..........
        return TRUE;
        }

        What is the reason of calling the base class member function

        CDialogEx::OnInitDialog()

        inside the child class member function?

        BOOL CMyDlg::OnInitDialog()

        V CPalliniC 2 Replies Last reply
        0
        • M Member_14575556

          Thank you. :)

          BOOL CMyDlg::OnInitDialog()
          {
          CDialogEx::OnInitDialog();
          ..........
          ..........
          return TRUE;
          }

          What is the reason of calling the base class member function

          CDialogEx::OnInitDialog()

          inside the child class member function?

          BOOL CMyDlg::OnInitDialog()

          V Offline
          V Offline
          Victor Nijegorodov
          wrote on last edited by
          #6

          All the dialog controls are created and subclassed within the base class CDialogEx::OnInitDialog()

          1 Reply Last reply
          0
          • M Member_14575556

            Thank you. That clear my doubt :) If you don't mind could you explain this line too:

            CHeaderCtrl* pHeader = (CHeaderCtrl*)m_ListControl.GetDlgItem(0);

            V Offline
            V Offline
            Victor Nijegorodov
            wrote on last edited by
            #7

            Where did you get this code? The better way to obtain the pointer to a header control is using [CListCtrl::GetHeaderCtrl](https://docs.microsoft.com/en-us/cpp/mfc/reference/clistctrl-class?view=vs-2019#getheaderctrl) method.

            M 1 Reply Last reply
            0
            • M Member_14575556

              Hello Everyone, I have some doubt regarding below code snippet.

              MyDlg::MyDlg(CWnd* pParent /*=NULL*/)
              : CDialogEx(IDD_DLG, pParent)
              , m_Name(_T(""))
              , m_Status(_T(""))
              , m_Comments(_T(""))
              {

              }

              Please explain why m_Name(_T("")),m_Status(_T("")),m_Comments(_T("")) is there. Thank you. :) I'm new to MFC VC++.

              V Offline
              V Offline
              Victor Nijegorodov
              wrote on last edited by
              #8

              Note that if these m_Name, m_Status, m_Comments are CString (CStringA, CStringW, CStringT) objects then you do not need to additionally initialize them to empty because they are already created (via default ctor) as empty strings.

              S 1 Reply Last reply
              0
              • M Member_14575556

                Thank you. :)

                BOOL CMyDlg::OnInitDialog()
                {
                CDialogEx::OnInitDialog();
                ..........
                ..........
                return TRUE;
                }

                What is the reason of calling the base class member function

                CDialogEx::OnInitDialog()

                inside the child class member function?

                BOOL CMyDlg::OnInitDialog()

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #9

                Code reuse. See, for instance: 11.6a — Calling inherited functions and overriding behavior | Learn C++[^].

                In testa che avete, signor di Ceprano?

                M 1 Reply Last reply
                0
                • V Victor Nijegorodov

                  Note that if these m_Name, m_Status, m_Comments are CString (CStringA, CStringW, CStringT) objects then you do not need to additionally initialize them to empty because they are already created (via default ctor) as empty strings.

                  S Offline
                  S Offline
                  Stefan_Lang
                  wrote on last edited by
                  #10

                  True, but I expect that in this case the compiler will optimze away the double initialization. All that remains is a little bit of extra code that, while not needed, serves to clarify that these members are indeed properly initialized - no matter what types these members really are. Moreover, if the types happen to change in the future, it's safer not to rely on them being initialized by default. It's similar to soem C++ keywords, like override: at the time you add it to your code, it doesn't serve any purpose but documentation. But if the virtual base method gets changed in the future, it's safer to use the override keyword because then the compiler will complain if you don't also adapt your override method(s).

                  GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                  1 Reply Last reply
                  0
                  • V Victor Nijegorodov

                    Where did you get this code? The better way to obtain the pointer to a header control is using [CListCtrl::GetHeaderCtrl](https://docs.microsoft.com/en-us/cpp/mfc/reference/clistctrl-class?view=vs-2019#getheaderctrl) method.

                    M Offline
                    M Offline
                    Member_14575556
                    wrote on last edited by
                    #11

                    Thanks for the reply. I got this code snippet from an MFC tutorial website.

                    void CMyDlg::ResetListControl() {
                    m_ListControl.DeleteAllItems();
                    int iNbrOfColumns;
                    CHeaderCtrl* pHeader = (CHeaderCtrl*)m_ListControl.GetDlgItem(0);/* <---- this line */
                    if (pHeader) {
                    iNbrOfColumns = pHeader->GetItemCount();
                    }
                    for (int i = iNbrOfColumns; i >= 0; i--) {
                    m_ListControl.DeleteColumn(i);
                    }
                    }

                    I know this code will reset all the Item and column in the list view control. I want to get a deeper understanding in that specific line of code.

                    V 1 Reply Last reply
                    0
                    • CPalliniC CPallini

                      Code reuse. See, for instance: 11.6a — Calling inherited functions and overriding behavior | Learn C++[^].

                      M Offline
                      M Offline
                      Member_14575556
                      wrote on last edited by
                      #12

                      First of all Thank you for the help. :) Is this concept similar for CWinApp::InitInstance . Even when I commented this line my application still works. why?

                      BOOL CWindowApp::InitInstance()
                      {
                      CWinApp::InitInstance();/* <--- this line */

                      CMyDlg dlg;
                      m_pMainWnd = &dlg;
                      INT_PTR nResponse = dlg.DoModal();
                      .......
                      .......
                      .......
                      }

                      I would like to ask one more question regarding m_pMainWnd. Sorry if this is a silly question. Why do we assign the address of the dialog to m_pMainWnd? I know m_pMainWnd is a CWnd* m_pMainWnd.

                      CPalliniC 1 Reply Last reply
                      0
                      • M Member_14575556

                        Thanks for the reply. I got this code snippet from an MFC tutorial website.

                        void CMyDlg::ResetListControl() {
                        m_ListControl.DeleteAllItems();
                        int iNbrOfColumns;
                        CHeaderCtrl* pHeader = (CHeaderCtrl*)m_ListControl.GetDlgItem(0);/* <---- this line */
                        if (pHeader) {
                        iNbrOfColumns = pHeader->GetItemCount();
                        }
                        for (int i = iNbrOfColumns; i >= 0; i--) {
                        m_ListControl.DeleteColumn(i);
                        }
                        }

                        I know this code will reset all the Item and column in the list view control. I want to get a deeper understanding in that specific line of code.

                        V Offline
                        V Offline
                        Victor Nijegorodov
                        wrote on last edited by
                        #13

                        Well, perhaps it is still working, but is there any guaranty it will also work in every new Windows version? What will happen if header control will get some other control ID?

                        1 Reply Last reply
                        0
                        • M Member_14575556

                          First of all Thank you for the help. :) Is this concept similar for CWinApp::InitInstance . Even when I commented this line my application still works. why?

                          BOOL CWindowApp::InitInstance()
                          {
                          CWinApp::InitInstance();/* <--- this line */

                          CMyDlg dlg;
                          m_pMainWnd = &dlg;
                          INT_PTR nResponse = dlg.DoModal();
                          .......
                          .......
                          .......
                          }

                          I would like to ask one more question regarding m_pMainWnd. Sorry if this is a silly question. Why do we assign the address of the dialog to m_pMainWnd? I know m_pMainWnd is a CWnd* m_pMainWnd.

                          CPalliniC Offline
                          CPalliniC Offline
                          CPallini
                          wrote on last edited by
                          #14

                          Quote:

                          Is this concept similar for CWinApp::InitInstance . Even when I commented this line my application still works. why?

                          There are two alternative explanations:

                          1. The CWinApp::InitInstance does nothing, so it is safe to discard its call.
                          2. InitInstance does perform some initialization that now is missing in your application. Your application may run fine, at the moment, (for instance because it doesn't need such initializations) but this is definitely not a safe scenario.

                          Bottom line: if they (Microsoft) do invoke CWinApp::InitInstance in their code samples (and in generated code) then I would do the same.

                          Quote:

                          hy do we assign the address of the dialog to m_pMainWnd? I know m_pMainWnd is a CWnd* m_pMainWnd.

                          Possibly because the dialog it is the application main window.

                          In testa che avete, signor di Ceprano?

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups