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. How does the using the address of operator work in Visual C++?

How does the using the address of operator work in Visual C++?

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestion
12 Posts 7 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.
  • T Offline
    T Offline
    Tom Moore
    wrote on last edited by
    #1

    Hi, I'd like to know how the 'address of (&)' applies to methods in Visual C++. Here is an example : void CMainWnd::OnRButtonDown(UINT nFlags, CPoint point) { CWindowDC dc(this); CRect rc; GetWindowRect(&rc); //.. } Whats the difference between : GetWindowRect(&rc); and GetWindowRect(rc); I would appreciate some more examples along with an explanation with what it does, why and how? I know I'm asking alot here but I would very appreciative. Thanks in Advance Tom

    B T I H P 7 Replies Last reply
    0
    • T Tom Moore

      Hi, I'd like to know how the 'address of (&)' applies to methods in Visual C++. Here is an example : void CMainWnd::OnRButtonDown(UINT nFlags, CPoint point) { CWindowDC dc(this); CRect rc; GetWindowRect(&rc); //.. } Whats the difference between : GetWindowRect(&rc); and GetWindowRect(rc); I would appreciate some more examples along with an explanation with what it does, why and how? I know I'm asking alot here but I would very appreciative. Thanks in Advance Tom

      B Offline
      B Offline
      BadKarma
      wrote on last edited by
      #2

      Hi, in the case u describe it is used to pass the object by address or by reference. It will depend on the signature of the called function. If I'm not mistaken this function GetWindowRect expects a pass by address (a pointer) because its based on the an API call which is written in plain C where there are no references

      int iValue = 3;
      int* pValue = &iValue; // pointer to value
      *pValue = 4;
      int& rValue = &iValue; // reference to value
      rValue = 5;
      

      Here you van see that when using a pointer (pValue) you need the to dereference it using the '*' operator; When using the reference (rValue) you can use it like you would have used iValue; Now this brings the following benefit, look at the following code:

      struct Point{
      int x;
      int y;
      };
      
      void FunctionA(Point ptMouse)
      {
        ptMouse.x = 100;
      }
      
      void FunctionB(Point* pMouse)
      {
        pMouse->x = 100;
      }
      
      void FunctionC(Point& rMouse)
      {
        rMouse.x = 100;
      }
      

      When using FunctionA the Point object will be copied onto the stack and in the function you only use this copy. [pass by value] When using FunctionB only the address is placed on the stack and inside the function you can access the original object. So when changing the object inside the function is will be changed outside the function (its the same object) [pass by address] When using FunctionC alsoo only the address is placed on the stack and inside we are using the original object. So when changing the object inside the function is will be changed outside the function. [pass by reference] When passing large objects (classes with many members, or large ones) the pass by value is not so good (slow, stack space consuming). So either pass by address or pass by reference are preferred. codito ergo sum

      T 1 Reply Last reply
      0
      • T Tom Moore

        Hi, I'd like to know how the 'address of (&)' applies to methods in Visual C++. Here is an example : void CMainWnd::OnRButtonDown(UINT nFlags, CPoint point) { CWindowDC dc(this); CRect rc; GetWindowRect(&rc); //.. } Whats the difference between : GetWindowRect(&rc); and GetWindowRect(rc); I would appreciate some more examples along with an explanation with what it does, why and how? I know I'm asking alot here but I would very appreciative. Thanks in Advance Tom

        T Offline
        T Offline
        Tom Moore
        wrote on last edited by
        #3

        I think I understand some of it now, but theres still some things that elude me. An example is CClient* pDC(this); CPen penRed = new CPen() penRed.Create(PS_SOLID,1,RGB(255,0,0); // Create a solid red pen CPen penOld = dc->SelectObject(&penRed); // Select the red pen and store the old pen in // penOld, but why use the address of it? dc.DrawText(300,400,penRed,"Hello World"); // Print Hello World dc.SelectObject(penOld) //Select the old object using the pointer I still am a bit confused like the previous snippet CRect rc; GetWindowRect(&rc); Thanks in advance again Tom

        G 1 Reply Last reply
        0
        • T Tom Moore

          Hi, I'd like to know how the 'address of (&)' applies to methods in Visual C++. Here is an example : void CMainWnd::OnRButtonDown(UINT nFlags, CPoint point) { CWindowDC dc(this); CRect rc; GetWindowRect(&rc); //.. } Whats the difference between : GetWindowRect(&rc); and GetWindowRect(rc); I would appreciate some more examples along with an explanation with what it does, why and how? I know I'm asking alot here but I would very appreciative. Thanks in Advance Tom

          I Offline
          I Offline
          includeh10
          wrote on last edited by
          #4

          hihi, u should learn C++ before u know GetWindowRect(). &rc is address of the rc. rc is rc. address means memory place in which the data of rc stored. it sounds very dangerous u go so far in programming.


          A nice tool for optimizing your Microsoft html-help contents. Includeh10

          T 1 Reply Last reply
          0
          • B BadKarma

            Hi, in the case u describe it is used to pass the object by address or by reference. It will depend on the signature of the called function. If I'm not mistaken this function GetWindowRect expects a pass by address (a pointer) because its based on the an API call which is written in plain C where there are no references

            int iValue = 3;
            int* pValue = &iValue; // pointer to value
            *pValue = 4;
            int& rValue = &iValue; // reference to value
            rValue = 5;
            

            Here you van see that when using a pointer (pValue) you need the to dereference it using the '*' operator; When using the reference (rValue) you can use it like you would have used iValue; Now this brings the following benefit, look at the following code:

            struct Point{
            int x;
            int y;
            };
            
            void FunctionA(Point ptMouse)
            {
              ptMouse.x = 100;
            }
            
            void FunctionB(Point* pMouse)
            {
              pMouse->x = 100;
            }
            
            void FunctionC(Point& rMouse)
            {
              rMouse.x = 100;
            }
            

            When using FunctionA the Point object will be copied onto the stack and in the function you only use this copy. [pass by value] When using FunctionB only the address is placed on the stack and inside the function you can access the original object. So when changing the object inside the function is will be changed outside the function (its the same object) [pass by address] When using FunctionC alsoo only the address is placed on the stack and inside we are using the original object. So when changing the object inside the function is will be changed outside the function. [pass by reference] When passing large objects (classes with many members, or large ones) the pass by value is not so good (slow, stack space consuming). So either pass by address or pass by reference are preferred. codito ergo sum

            T Offline
            T Offline
            Tom Moore
            wrote on last edited by
            #5

            Thanks I think I understand! The reason why is? When windows was created using GDI things were written in good old C! Unlike C++, C didnt use pass references, so you had to use pass by address. Also since pass by Value such as : CClientDC dc(this); CPen penBlue = new CPen(); penBlue.Create(PS_SOLID,1,RGB(0,0,255)); dc.SelectObject(penBlue) // pass by value, makes a copy - uses up a lot of memory (BAD) //instead use dc.SelectObject(&penBlue) // pass by address - fast and effecient, using original object (GOOD) also you could use CPen& rPenBlue = &penBlue; dc.SelectObject(rPenBlue) // not sure if this is right but pass by reference Thanks Tom

            P 1 Reply Last reply
            0
            • I includeh10

              hihi, u should learn C++ before u know GetWindowRect(). &rc is address of the rc. rc is rc. address means memory place in which the data of rc stored. it sounds very dangerous u go so far in programming.


              A nice tool for optimizing your Microsoft html-help contents. Includeh10

              T Offline
              T Offline
              Tom Moore
              wrote on last edited by
              #6

              Hi, I do know C++! I did a programming course on it, but I covered a small portion of address of operator in it, and we didnt cover address of operator with objects. Thanks Tom

              1 Reply Last reply
              0
              • T Tom Moore

                Hi, I'd like to know how the 'address of (&)' applies to methods in Visual C++. Here is an example : void CMainWnd::OnRButtonDown(UINT nFlags, CPoint point) { CWindowDC dc(this); CRect rc; GetWindowRect(&rc); //.. } Whats the difference between : GetWindowRect(&rc); and GetWindowRect(rc); I would appreciate some more examples along with an explanation with what it does, why and how? I know I'm asking alot here but I would very appreciative. Thanks in Advance Tom

                H Offline
                H Offline
                Hamid Taebi
                wrote on last edited by
                #7

                Hi Tom Moore, int* m_pNumber1; //this operator is pointer int* m_pNumber2;//this operator is pointer int m_Number1;//It's a integer int m_Number2;//It's a integer m_Number1=20;//Value is Integer m_Number2=30;//Value is Integer The Follow these statement Address of Operator m_Number1 and m_Number2 m_pNumber1=&m_Number1;//////////& is Address of Operator m_pNumber2=&m_Number2; ////////////////////////Now int iResult=m_Number1+ m_Number2;///->>>>>>>>Result=50 iResult=*m_pNumber1+ *m_pNumber2;///->>>>>>>>Result=50

                1 Reply Last reply
                0
                • T Tom Moore

                  I think I understand some of it now, but theres still some things that elude me. An example is CClient* pDC(this); CPen penRed = new CPen() penRed.Create(PS_SOLID,1,RGB(255,0,0); // Create a solid red pen CPen penOld = dc->SelectObject(&penRed); // Select the red pen and store the old pen in // penOld, but why use the address of it? dc.DrawText(300,400,penRed,"Hello World"); // Print Hello World dc.SelectObject(penOld) //Select the old object using the pointer I still am a bit confused like the previous snippet CRect rc; GetWindowRect(&rc); Thanks in advance again Tom

                  G Offline
                  G Offline
                  Gary R Wheeler
                  wrote on last edited by
                  #8

                  CClient* pDC(this);
                  **CPen ***penRed = new CPen();

                  **penRed->**Create(PS_SOLID,1,RGB(255,0,0)); // Create a solid red pen

                  CPen *penOld = dc->SelectObject(penRed); // Select the red pen and store the old pen in // penOld, but why use the address of it?

                  //dc.DrawText(300,400,penRed,"Hello World"); // Print Hello World

                  dc.SelectObject(penOld) //Select the old object using the pointer

                  I've marked up the code you posted. Notice the bold areas. 1. The new operator returns the address of an object allocated on the heap. The value on the left side of the assignment therefore has to be a pointer variable. 2. If you refer to a member of an object through a pointer, you have to use the -> operator, as in penRed->Create. 3. The SelectObject function in the CDC class takes a pointer to a CGdiObject. 4. The CDC class doesn't have a DrawText member that takes a pen argument.


                  Software Zen: delete this;

                  Fold With Us![^]

                  1 Reply Last reply
                  0
                  • T Tom Moore

                    Hi, I'd like to know how the 'address of (&)' applies to methods in Visual C++. Here is an example : void CMainWnd::OnRButtonDown(UINT nFlags, CPoint point) { CWindowDC dc(this); CRect rc; GetWindowRect(&rc); //.. } Whats the difference between : GetWindowRect(&rc); and GetWindowRect(rc); I would appreciate some more examples along with an explanation with what it does, why and how? I know I'm asking alot here but I would very appreciative. Thanks in Advance Tom

                    T Offline
                    T Offline
                    Tom Moore
                    wrote on last edited by
                    #9

                    Thanks to everyone! I think I understand everything now :) You use address of the object to access the originl object, eg. dc.SelectObject(&penBlue); // Uses the original CPen plus the Windows GDI libary is written in Pure C therefore it requires the address of the object. Where as dc.SelectObject(penBlue); // Creates a copy and is slow. I think GDI+ was written in either .NET or C++ instead of C and therefore is slightly simpler to use. Thanks again Tom

                    1 Reply Last reply
                    0
                    • T Tom Moore

                      Hi, I'd like to know how the 'address of (&)' applies to methods in Visual C++. Here is an example : void CMainWnd::OnRButtonDown(UINT nFlags, CPoint point) { CWindowDC dc(this); CRect rc; GetWindowRect(&rc); //.. } Whats the difference between : GetWindowRect(&rc); and GetWindowRect(rc); I would appreciate some more examples along with an explanation with what it does, why and how? I know I'm asking alot here but I would very appreciative. Thanks in Advance Tom

                      P Offline
                      P Offline
                      PJ Arends
                      wrote on last edited by
                      #10

                      Tom Moore wrote:

                      Whats the difference between : GetWindowRect(&rc); and GetWindowRect(rc);

                      The function declaration for GetWindowRect is CWnd::GetWindowRect(LPRECT lpRect) const; so it takes a pointer to a RECT structure as it's parameter. The reason why you can pass a CRect object, either as a pointer or by 'reference' (remember that the values of the window rectangle are copied into the CRect object, so you can not pass it in using the copy c'tor) is because the CRect class is derived from the RECT structure and has member operator LPRECT. If you pass it by address, GetWindowRect() assumes that you passed in the address of a RECT structure, or the base class, and it works from there. If you pass it by reference, then the CRect::operator LPRECT() kicks in to pass it's own address ( return this; ). It's all in the magic of overloaded operators. So the difference between the two examples you gave is that there is really no difference functionally, although passing by address will save a few CPU cycles as it saves a call to the CRect::operator LPRECT


                      You may be right I may be crazy But it just may be a lunatic you’re looking for -- Billy Joel -- Within you lies the power for good - Use it!

                      1 Reply Last reply
                      0
                      • T Tom Moore

                        Thanks I think I understand! The reason why is? When windows was created using GDI things were written in good old C! Unlike C++, C didnt use pass references, so you had to use pass by address. Also since pass by Value such as : CClientDC dc(this); CPen penBlue = new CPen(); penBlue.Create(PS_SOLID,1,RGB(0,0,255)); dc.SelectObject(penBlue) // pass by value, makes a copy - uses up a lot of memory (BAD) //instead use dc.SelectObject(&penBlue) // pass by address - fast and effecient, using original object (GOOD) also you could use CPen& rPenBlue = &penBlue; dc.SelectObject(rPenBlue) // not sure if this is right but pass by reference Thanks Tom

                        P Offline
                        P Offline
                        PJ Arends
                        wrote on last edited by
                        #11

                        Tom Moore wrote:

                        CClientDC dc(this); CPen penBlue = new CPen(); penBlue.Create(PS_SOLID,1,RGB(0,0,255)); dc.SelectObject(penBlue) // pass by value, makes a copy - uses up a lot of memory (BAD)

                        Actually, this code will not even compile. CDC::SelectObject expects a CPen* pointer, so you have to pass by address.


                        You may be right I may be crazy But it just may be a lunatic you’re looking for -- Billy Joel -- Within you lies the power for good - Use it!

                        1 Reply Last reply
                        0
                        • T Tom Moore

                          Hi, I'd like to know how the 'address of (&)' applies to methods in Visual C++. Here is an example : void CMainWnd::OnRButtonDown(UINT nFlags, CPoint point) { CWindowDC dc(this); CRect rc; GetWindowRect(&rc); //.. } Whats the difference between : GetWindowRect(&rc); and GetWindowRect(rc); I would appreciate some more examples along with an explanation with what it does, why and how? I know I'm asking alot here but I would very appreciative. Thanks in Advance Tom

                          D Offline
                          D Offline
                          David Crow
                          wrote on last edited by
                          #12

                          In C, variables can be passed to a function either by-value or by-reference. For example:

                          // pass-by-value
                          void foo( int num )
                          {
                          num = 12;
                          }
                          ...
                          int x = 5;
                          printf("%x\n", x); // will print 5
                          foo(x);
                          printf("%x\n", x); // will also print 5

                          // pass-by-reference
                          void foo( int *num )
                          {
                          *num = 12; // change the value pointed to by 'num'
                          }
                          ...
                          int x = 5;
                          printf("%x\n", x); // will print 5
                          foo(&x);
                          printf("%x\n", x); // will now print 12

                          Another consideration is the amount of memory pushed onto the stack when one or the other is used. For integral types such as int, char, and double, there is little, if any, difference between the two. A char is 1 byte, a double is 8 bytes, and a pointer to either is 4 bytes. Where this matters more is when large data structures are being passed to a function. Imagine an object that is 1497 bytes in size. Passing the object by-value will result in 1497 bytes being piushed onto the stack, whereas passing the object by-reference will result in only 4 bytes being pushed onto the stack.


                          "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

                          "There is no death, only a change of worlds." - Native American Proverb

                          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