Results 1 to 6 of 6

Thread: References

  1. #1

    Thread Starter
    Frenzied Member vbgladiator's Avatar
    Join Date
    May 2001
    Posts
    1,950

    References

    Hi,

    I'm looking into C++ more and more and I came across references. What the heck are they useful for?? I mean why create a reference when you can just work with the original?


    Thanks,
    Don't anthropomorphize computers -- they hate it

  2. #2
    Hyperactive Member Comreak's Avatar
    Join Date
    Feb 2001
    Location
    Dis
    Posts
    319
    You use references for the same reason you use pointers: the ability to keep track of objects in memory. Sometimes working with the original object isn't feasible. This is really evident with dynamic instantiation were the only method for accessing objects is through pointers.

    When you first learn about them, references and pointers aren't immediately useful. If you want to get frighteningly familiar with pointers, check out some of the linked list tutorials on the web.
    Last edited by Comreak; Dec 3rd, 2003 at 05:54 PM.
    C.O.M.R.E.A.K.: Cybernetic Obedient Machine Responsible for Exploration and Accurate Killing

  3. #3
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    A basic use for using references is this: suppose I have a function that verifies that all the data in my array is ok, but doesn't change it.

    Code:
    bool CheckArray(vector<int> array)
    {
       // ....
    }
    
    //
    int main() 
    {
       vector<int> myArray = CreateReallyBigArray();
    
       // not good.  myArray is copied.
       if (CheckArray(myArray)) { // .... };
    }
    When I call CheckArray(), the contents of "myArray" will be copied into "array". This can take a lot of time. By using references, I can simply use the same array that I have in main(), and by marking it const, show that I'm not going to change it inside the function:

    Code:
    bool CheckArray(const vector<int>& array)
    {
       // ....
    }
    
    //
    int main() 
    {
       vector<int> myArray = CreateReallyBigArray();
    
       // array is a reference to myArray.  Only it's address on the stack / in memory is passed.
       if (CheckArray(myArray)) { // .... };
    }
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    references are a form of indirection, they're useful for the same reason links are useful on the web
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    Hyperactive Member
    Join Date
    Jul 2002
    Location
    WGTN, New Zealand
    Posts
    338
    Exactly! Wouldn't want the entire internet being copied to your computer if you just one to look at one page.

  6. #6
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Originally posted by Dreamlax
    Exactly! Wouldn't want the entire internet being copied to your computer if you just one to look at one page.
    I want the internet copied to my computer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width