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. Reference to Variables?

Reference to Variables?

Scheduled Pinned Locked Moved C / C++ / MFC
question
4 Posts 3 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.
  • Y Offline
    Y Offline
    ypo_cnihtmarcher
    wrote on last edited by
    #1

    What is the advantage of using a reference over the variable. Can't you just pass the variable name instead of passing a reference to the variable.:confused: KAI_YPO

    C 1 Reply Last reply
    0
    • Y ypo_cnihtmarcher

      What is the advantage of using a reference over the variable. Can't you just pass the variable name instead of passing a reference to the variable.:confused: KAI_YPO

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      When you pass a variable, a copy of that variable is made. When you pass a reference, the original variable is passed in. This means two things: 1. If the variable is big, you save some effort in making a copy 2. Anything you do to the variable in your function will effect the variable that was passed in so if you have two functions: void Inc(int i) { ++i; } and void IncRef(int & i) { ++i; } In your main code you can do this: int i = 0; Inc(i); // i still = 0, because only the i in the function was incremented IncRef(i); // i now = 1, as this i is the same one that was incremented in the function. Note, the variable names do not need to be the same. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder

      G 1 Reply Last reply
      0
      • C Christian Graus

        When you pass a variable, a copy of that variable is made. When you pass a reference, the original variable is passed in. This means two things: 1. If the variable is big, you save some effort in making a copy 2. Anything you do to the variable in your function will effect the variable that was passed in so if you have two functions: void Inc(int i) { ++i; } and void IncRef(int & i) { ++i; } In your main code you can do this: int i = 0; Inc(i); // i still = 0, because only the i in the function was incremented IncRef(i); // i now = 1, as this i is the same one that was incremented in the function. Note, the variable names do not need to be the same. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder

        G Offline
        G Offline
        GeMe_Hendrix
        wrote on last edited by
        #3

        Here's a question though... Which is more efficient... void IncRef(int & i) { ++i; } or this... void IncRef(int i) { ++i; } The way I think about it is we are still passing pointer to another variable in a previous stack frame. Therefore in this case would it make any difference performance wise since we are still just dealing with a number whether it be a memory location or an integer. I know that if this was a pointer I would be correct in this case but I'm not sure about references. Please feedback though I may be wrong.

        C 1 Reply Last reply
        0
        • G GeMe_Hendrix

          Here's a question though... Which is more efficient... void IncRef(int & i) { ++i; } or this... void IncRef(int i) { ++i; } The way I think about it is we are still passing pointer to another variable in a previous stack frame. Therefore in this case would it make any difference performance wise since we are still just dealing with a number whether it be a memory location or an integer. I know that if this was a pointer I would be correct in this case but I'm not sure about references. Please feedback though I may be wrong.

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          In this case, it would only be barely more efficient to pass a reference. It's not just passing a number, it's also allocating and deallocating the memory. But your app won't hang on this, it's more if you have huge data structures that you want to pass into a function to be read ( and this is what keyword 'const' is for ), that you can make some serious gains in efficiency by using references. Christian I have drunk the cool-aid and found it wan and bitter. - Chris Maunder

          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