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
14
votes
1 answer

Visual C++ 10.0 bug in std::reference_wrapper?

Code: #include struct Foo { virtual void mf() = 0; }; struct Bar: Foo { virtual void mf() {} }; int main() { Bar o; std::reference_wrapper wrapper( o ); } Result with MinGW g++ 4.6.1: [d:\dev\test] > g++…
14
votes
2 answers

Intuitive understanding of functions taking references of references

Possible Duplicate: What does T&& mean in C++11? For some reason, this is eluding my intuition, and I cannot find any explanation on the internet. What does it mean for a C++ function to take a reference of a reference? For example: void…
Daniel
  • 6,595
  • 9
  • 38
  • 70
14
votes
1 answer

How to pass reference-to-function into another function

I have been reading about function pointers and about using them as parameters for other functions. My question is how would you pass a function by reference without using pointers? I have been trying to find the answer on the Internet but I haven't…
Hudson Worden
  • 2,263
  • 8
  • 30
  • 45
14
votes
4 answers

How to modify an array in function?

MATLAB is a pass by value language. I have a recursive function that processes pixel's neighbors. It is very expensive to make the copy of the image (in my case two images) each time the function is called. I used global variables to solve the…
nimcap
  • 10,062
  • 15
  • 61
  • 69
14
votes
1 answer

Pass an argument by reference in C++/CLI so re-assignment affects the caller

Probably this is not a difficult question, but I am always a little bit confused on how to treat String type as an argument in Visual C++. I have the following to functions: void function_1(String ^str_1) { str_1 = gcnew String("Test"); } void…
stefangachter
  • 845
  • 2
  • 10
  • 16
14
votes
1 answer

Passing strings by reference and value in C++

I want to declare a string, initialize it by passing it by reference, and then pass it by value to the 'outputfile' function. The code below works, but I don't know why. In main I would expect to pass the string 'filename'…
Peter
  • 367
  • 1
  • 4
  • 12
14
votes
1 answer

passing a const char instead of a std::string as function argument

This is a newbie question but I cannot understand how it works. Suppose I have the function like the one below void foo(const std::string& v) { cout << v << endl; } And the call below in my program. foo("hi!"); Essentially I am passing a const…
Abruzzo Forte e Gentile
  • 14,423
  • 28
  • 99
  • 173
14
votes
5 answers

Java - Object state does not change after method call

Beginner java question, but I cannot understand how call-by-Value ( or Reference ) is working in the example below - How come the String value is not modified after it exits the method while my custom String Object is. ? Same with other classes…
Anand Hemmige
  • 3,593
  • 6
  • 21
  • 31
13
votes
5 answers

Implications of using an ampersand before a function name in C++?

Given the example: inline string &GetLabel( ) { return m_Label; }; Where m_Label is a private class member variable. The way I think I understand it, this function will return a reference to the variable m_Label. What would be the…
Cuthbert
  • 2,908
  • 5
  • 33
  • 60
13
votes
3 answers

Use variable by reference in arrow function

PHP 7.4 introduced Arrow functions. And it also introduced implicit by-value scope binding which eliminate the need for use keyword. Now if we want to use a variable out of a closure's scope by reference with a regular anonymous function we would…
Rain
  • 3,416
  • 3
  • 24
  • 40
13
votes
5 answers

pass by reference and value with pointers

I don't understand why passing a pointer to a function doesn't change the data being passed in. If the function proto looked like this: void func( int *p ); and func allocated memory to p, why can't it be used outside of the function? I thought…
TheFuzz
  • 2,607
  • 6
  • 29
  • 48
13
votes
3 answers

Obtaining a pointer to the end of an array

I use the following template to obtain a pointer pointing after the last element of an array: template T* end_of(T (&array)[n]) { return array + n; } Now I seem to remember that there was some problem with this approach,…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
13
votes
2 answers

Why is passing by const ref slower when using std::async

As an exercise to learn about std::async I wrote a small program that calculates the sum of a large vector, distributed about a lot of threads. My code below is as follows #include #include #include #include…
Bernhard
  • 3,619
  • 1
  • 25
  • 50
13
votes
2 answers

java Boolean value not changing in called method

I have a scenario where I want to set a Boolean object and then use its booleanValue() in a constructor later on in the method. However, the scope in which the object is set is different. It is set in a method called by the method in which the…
user1849060
  • 621
  • 3
  • 10
  • 20
13
votes
9 answers

Can I pass a primitive type by reference in Java?

I would like to call a method which could potentially take on different versions, i.e. the same method for input parameters that are of type: boolean byte short int long The way I would like to do this is by "overloading" the method (I think that…
Hristo
  • 45,559
  • 65
  • 163
  • 230