| | |
Reference Variable
Please support our C++ advertiser: Intel Parallel Studio Home
0
•
•
•
•
The vale of n will be 6.
This is because any operation you do on a reference, it also acts on its referenced variable.
Please, if you have a question, don't start a thread as a code snippet, start it as a forum thread. thanx
This is because any operation you do on a reference, it also acts on its referenced variable.
Please, if you have a question, don't start a thread as a code snippet, start it as a forum thread. thanx
0
•
•
•
•
The absolute easiest way to test this is to write a small console app with those code elements in it and step through the execution and watch what happens to the variable.
If you need a compiler:
http://www.microsoft.com/express
If you need a compiler:
http://www.microsoft.com/express
0
•
•
•
•
...a reference is treated the same as the original:
http://www.cprogramming.com/tutorial/references.html :
C++ references allow you to create a second name for the a variable that you can use to read or modify the original data stored in that variable. While this may not sound appealing at first, what this means is that when you declare a reference and assign it a variable, it will allow you to treat the reference exactly as though it were the original variable for the purpose of accessing and modifying the value of the original variable--even if the second name (the reference) is located within a different scope. This means, for instance, that if you make your function arguments references, and you will effectively have a way to change the original data passed into the function. This is quite different from how C++ normally works, where you have arguments to a function copied into new variables. It also allows you to dramatically reduce the amount of copying that takes place behind the scenes, both with functions and in other areas of C++, like catch clauses.
C++ Syntax (Toggle Plain Text)
int main(void) { int m=5; int &n=m; printf("m=%d n=%d\n", m, n); // m=5 n=5 ++n; // increments BOTH n and m printf("m=%d n=%d\n", m, n); // m=6 n=6 return 0; }
http://www.cprogramming.com/tutorial/references.html :
C++ references allow you to create a second name for the a variable that you can use to read or modify the original data stored in that variable. While this may not sound appealing at first, what this means is that when you declare a reference and assign it a variable, it will allow you to treat the reference exactly as though it were the original variable for the purpose of accessing and modifying the value of the original variable--even if the second name (the reference) is located within a different scope. This means, for instance, that if you make your function arguments references, and you will effectively have a way to change the original data passed into the function. This is quite different from how C++ normally works, where you have arguments to a function copied into new variables. It also allows you to dramatically reduce the amount of copying that takes place behind the scenes, both with functions and in other areas of C++, like catch clauses.
Similar Threads
- Writing a Program using "pass-by-reference" functions. (C++)
- A good idea when to use pointers vs "reference variables"? (C++)
- Reference types (VB.NET)
- const reference variable declaration more efficient ? (C++)
- Is it possible to pass back both a return variable & reference variable in one funct? (C++)
- Exact meaning of functions returning reference (C++)
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets



