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. Object pointer as method argument

Object pointer as method argument

Scheduled Pinned Locked Moved C / C++ / MFC
debuggingquestion
6 Posts 3 Posters 1 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.
  • _ Offline
    _ Offline
    _Flaviu
    wrote on last edited by
    #1

    I have a CData derived from CObject. In my CDocument, I have a CMap, with key as int, and values as CData*. In a CMyDocument method I have:

    void CMyDocument::GetData(int nItemData, CData* pData/* = NULL*/)
    {
    m_MapData.Lookup(nItemData, pData);
    TRACE("Inside doc method: pData is not NULL: %d\n", NULL != pData);
    }

    and in my CMyView:

    int nItemData = 1;
    CData\* pData = NULL;
    GetDocument()->GetData(nItemData, pData));
    TRACE("In MyView: pData is not NULL: %d\\n", NULL != pData);
    

    and here is the result: Inside doc method: pData is not NULL: 1 In MyView: pData is not NULL: 0 Why inside CMyDocument I have a valid pData, but outside not ?

    L CPalliniC 2 Replies Last reply
    0
    • _ _Flaviu

      I have a CData derived from CObject. In my CDocument, I have a CMap, with key as int, and values as CData*. In a CMyDocument method I have:

      void CMyDocument::GetData(int nItemData, CData* pData/* = NULL*/)
      {
      m_MapData.Lookup(nItemData, pData);
      TRACE("Inside doc method: pData is not NULL: %d\n", NULL != pData);
      }

      and in my CMyView:

      int nItemData = 1;
      CData\* pData = NULL;
      GetDocument()->GetData(nItemData, pData));
      TRACE("In MyView: pData is not NULL: %d\\n", NULL != pData);
      

      and here is the result: Inside doc method: pData is not NULL: 1 In MyView: pData is not NULL: 0 Why inside CMyDocument I have a valid pData, but outside not ?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      In case 1 you have not shown the value of pData. In case 2 you have explicitly set its value to NULL.

      _ 1 Reply Last reply
      0
      • L Lost User

        In case 1 you have not shown the value of pData. In case 2 you have explicitly set its value to NULL.

        _ Offline
        _ Offline
        _Flaviu
        wrote on last edited by
        #3

        In case 1, the trace show me that pData is not NULL, and after GetData call, I set up nothing, I only display if pData is NULL or not ... but is NULL ...

        L 1 Reply Last reply
        0
        • _ _Flaviu

          I have a CData derived from CObject. In my CDocument, I have a CMap, with key as int, and values as CData*. In a CMyDocument method I have:

          void CMyDocument::GetData(int nItemData, CData* pData/* = NULL*/)
          {
          m_MapData.Lookup(nItemData, pData);
          TRACE("Inside doc method: pData is not NULL: %d\n", NULL != pData);
          }

          and in my CMyView:

          int nItemData = 1;
          CData\* pData = NULL;
          GetDocument()->GetData(nItemData, pData));
          TRACE("In MyView: pData is not NULL: %d\\n", NULL != pData);
          

          and here is the result: Inside doc method: pData is not NULL: 1 In MyView: pData is not NULL: 0 Why inside CMyDocument I have a valid pData, but outside not ?

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

          Because the CData pointer is passed by value. If you need to modify the pointer then you have to pass it by reference (or use a double pointer).

          In testa che avete, signor di Ceprano?

          _ 1 Reply Last reply
          0
          • _ _Flaviu

            In case 1, the trace show me that pData is not NULL, and after GetData call, I set up nothing, I only display if pData is NULL or not ... but is NULL ...

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Your test does not show what you think. It shows the values that you set in pData before the call to GetData, which does not change it. You need to reread the documentation, and understand the difference between a pointer and a reference.

            1 Reply Last reply
            0
            • CPalliniC CPallini

              Because the CData pointer is passed by value. If you need to modify the pointer then you have to pass it by reference (or use a double pointer).

              _ Offline
              _ Offline
              _Flaviu
              wrote on last edited by
              #6

              I used double pointer and goes perfectly. Kindly thank you all of you (including Richard)!

              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