pass by reference and pass by value in c++ and java
Possible Duplicates:
What's the difference between passing by reference vs. passing by value?
Java and C++ pass by value and pass by reference
Java is "pass by value" or "pass by reference"?
and what about c++ and c? is c++ and c "passed by value" or "passed by reference"?
and what is the difference between "pass by value" or "pass by reference"?
Java is pass by value, but the value you pass may be a reference.
In C++, you choose whether to pass by value or by reference. In C, you always pass by value, and there is no such thing as a reference; you have to pass a pointer to do something similar.
If you pass by value, the function gets a copy of the variable you pass. If you change the copy, the original value remains the same (though, it the value passed was itself a reference, you may change the referred object).
If you pass by reference, the function gets the actual variable passed. If the function changes that variable, you are actually changing the variable that was passed to the function.
Java is "pass by value." Always.
C++ allows pass by reference. Everything in C is passed by value.
When you pass a variable by reference to a function, the function can change the value of the variable in the caller. When you pass by value, the function gets a copy of the variable.
Java is pass by value for basic types (int, double), and pass by reference for any class inheriting from Object.
C++ is pass however you like.
pass by value makes a copy of the parameter, so any changes made in the function only have an effect inside that function.
pass by reference doesn't copy, so any changes made to that parameter within the subfunction will effect the value outside of that function.
链接地址: http://www.djcxy.com/p/20604.html上一篇: 按引用和指针传递