Questions tagged [pass-by-reference]

Pass by reference is an argument marshalling strategy whereby a variable's location in memory is passed to a function, rather than a copy of the variable's value, although the function appears in the source code to receive the variable itself rather than a pointer to it.

Passing by reference means that the memory address of a variable is passed rather than a copy of the variable's value.

This typically means that the function can modify the passed variable, assigning a new value to it. However for performance reasons, passing by reference may be useful even if the passed structure is not modified, as with the Pascal var modifier, and some programming languages have constructs (like the C const modifier) to disallow modification of a variable passed by reference.

4485 questions
9
votes
10 answers

Swap two strings in Java, by passing them to a utility function, but without returning objects or using wrapper classes

I am trying to swap two strings in Java. I never really understood "strings are immutable". I understand it in theory, but I never came across it in practice. Also, since String is an object in Java and not a primitive type, I don't understand why…
anon355079
9
votes
1 answer

std::thread constructor Is there a difference between passing a pointer and passing by ref?

When creating a thread that calls a member function, is there a difference between passing a pointer to the current class or passing a reference? From the example below, does method1 behave the same as method2? Are there any differences? class…
9
votes
2 answers

Passing vector of vectors to function

I have a function which accepts a vector> and modifies the MyClass instances. It's been a long time since I've written any C++ and I'm having difficulty remembering what is sufficient here for passing the entire arg by reference…
9
votes
4 answers

How is its lifetime of a return value extended to the scope of the calling function when it is bound to a const reference in the calling function?

"If you return a value (not a reference) from the function, then bind it to a const reference in the calling function, its lifetime would be extended to the scope of the calling function." So: CASE A const BoundingBox Player::GetBoundingBox(void) { …
brainydexter
  • 19,826
  • 28
  • 77
  • 115
9
votes
1 answer

Issue with cloning and pass-by-reference

So for the past few days I have been tearing my hair out trying to get a class to clone properly. The problem is that cloning doesn't remove/redo any of the pass-by-reference. The result is, that the main data object is still passed as a reference,…
Nicholas Summers
  • 4,444
  • 4
  • 19
  • 35
9
votes
3 answers

C++ Passing std::string by reference to function in dll

I have the problem with passing by reference std::string to function in dll. This is function call: CAFC AFCArchive; std::string sSSS =…
Aleksey
  • 140
  • 1
  • 8
9
votes
6 answers

Assigning to a new value to an object passed into a function in JavaScript

I'm new to JavaScript (though experienced in C++), and today, I wrote something like this: function foo(bar) { bar = "something else"; } var x = "blah"; foo(x); alert(x); // Alerts with "blah", but I was expecting it to alert with "something…
oggmonster
  • 4,672
  • 10
  • 51
  • 73
9
votes
4 answers

Why am I getting "Only variables should be passed by reference" error?

Check out this code: $last = end($p = explode('/', $someString)); Getting this notice: Only variables should be passed by reference I'm really confused because $p is a variable.
thelolcat
  • 10,995
  • 21
  • 60
  • 102
9
votes
5 answers

JS object copy by value vs copy by reference

I was playing with chrome console and noticed something which I couldn't understand. I know in JS variables are copied by value and objects are copied by reference. Below code works fine as expected which outputs 2 and proves JS objects work as…
RuntimeException
  • 1,135
  • 2
  • 11
  • 25
9
votes
1 answer

Passing reference to activity to utility class android

I realise this question has been asked many times, but I am still unable to completely understand this concept. In my application, I am using a static utility class to keep common methods (like showing error dialogs) Here is how my static class…
TMS
  • 1,201
  • 1
  • 13
  • 20
9
votes
2 answers

Passing return value of a function as reference

What is supposed to happen in the following case: int functionA() { return 25; } void functionB(const int& ref) { cout << ref << endl; } void start() { functionB(functionA()); } When compiling this example, it outputs the correct…
maxdev
  • 2,491
  • 1
  • 25
  • 50
9
votes
4 answers

Javascript Variables as Object Pointers

I have a question out of curiosity. So I looked into how JS handles variable assignment and I get it. How does variable assignment work in JavaScript? But the same principle doesn't seem to exhibit itself in the following code I am working on: var…
nemo
  • 593
  • 2
  • 8
  • 22
9
votes
7 answers

Using REF & OUT keywords with Passing by Reference & Passing by Value in C#

Here is what I understand so far: PASS BY VALUE Passing by value means a copy of an argument is passed. Changes to that copy do not change the original. PASS BY REFERENCE Passing by reference means a reference to the original is passed. changes to…
Goober
  • 13,146
  • 50
  • 126
  • 195
9
votes
3 answers

Inexplicable expense related to getting data out of a function in optimized and timed C++ code

I have written optimized code for an algorithm that computes a vector of quantities. I have timed it before and after various attempts at getting the data computed in the function out of the function. I think that the specific nature of the…
rg6
  • 329
  • 2
  • 10
9
votes
7 answers

Passing a parameter versus returning it from function

As it might be clear from the title which approach should we prefer? Intention is to pass a few method parameters and get something as output. We can pass another parameter and method will update it and method need not to return anything now,…
instanceOfObject
  • 2,936
  • 5
  • 49
  • 85