|
-
Dec 3rd, 2003, 04:37 PM
#1
Thread Starter
Frenzied Member
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
-
Dec 3rd, 2003, 05:51 PM
#2
Hyperactive Member
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
-
Dec 3rd, 2003, 06:45 PM
#3
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.
-
Dec 4th, 2003, 04:08 AM
#4
transcendental analytic
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.
-
Dec 6th, 2003, 05:58 AM
#5
Hyperactive Member
Exactly! Wouldn't want the entire internet being copied to your computer if you just one to look at one page.
-
Dec 6th, 2003, 10:30 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|