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
11
votes
3 answers

What is the purpose of using a reference to a reference in C++?

In my adventures studying the boost libraries, I've come across function signatures that have parameters which are a reference to a reference to an object. Example: void function(int && i); What is the purpose/benefit of doing it this way rather…
Nick Strupat
  • 4,928
  • 4
  • 44
  • 56
11
votes
3 answers

How to pass structure by reference?

If I have some existing struct, but I want to use "Reference" behavior, how do I achieve that? I can write some simple class-holder like class Box { var value: T init(_ value: T) { self.value = value } } I guess there must be…
vbezhenar
  • 11,148
  • 9
  • 49
  • 63
11
votes
3 answers

C# 4.0 'dynamic' doesn't set ref/out arguments

I'm experimenting with DynamicObject. One of the things I try to do is setting the values of ref/out arguments, as shown in the code below. However, I am not able to have the values of i and j in Main() set properly (even though they are set…
Buu
  • 49,745
  • 5
  • 67
  • 85
11
votes
2 answers

Can I force `const` to pass by reference (aka the missing `in` parameter)

Delphi has: var : pass by reference; parameter is both input and output. out : pass by reference; parameter is output only. const: pass by ..... well it depends; parameter is input only. in : pass by reference; parameter is input only and will…
Johan
  • 74,508
  • 24
  • 191
  • 319
11
votes
8 answers

pass strings by reference in C

I'm having trouble figuring out how to pass strings back through the parameters of a function. I'm new to programming, so I imagine this this probably a beginner question. Any help you could give would be most appreciated. This code seg faults, and…
Jenna
  • 53
  • 1
  • 1
  • 5
11
votes
5 answers

Pass a reference to a reference

I think it's illegal to pass a reference to a reference in C++.However ,when I run this code it gives me no error. void g(int& y) { std::cout << y; y++; } void f(int& x) { g(x); } int main() { int a = 34; f(a); return 0; …
devsaw
  • 1,007
  • 2
  • 14
  • 28
11
votes
3 answers

Why is function call by reference in PHP deprecated?

I wrote the following code: "; fix_names($a1, $a2, $a3); echo $a1." ".$a2." ".$a3; function fix_names(&$n1, &$n2, &$n3) { …
Muhammad Maqsoodur Rehman
  • 33,681
  • 34
  • 84
  • 124
11
votes
3 answers

Passing objects by reference vs value

I just want to check my understanding of C#'s ways of handling things, before I delve too deeply into designing my classes. My current understanding is that: Struct is a value type, meaning it actually contains the data members defined…
Kyle Baran
  • 1,793
  • 2
  • 15
  • 30
11
votes
1 answer

How to access variable by id?

Possible Duplicate: Get object by id()? >>> var = 'I need to be accessed by id!' >>> address = id(var) >>> print(address) 33003240 Is there any way to use address of var in memory, provided by id(), for accessing value of var? UPD: I also want…
Gill Bates
  • 14,330
  • 23
  • 70
  • 138
11
votes
8 answers

swapping of objects in java

I have studied that java is pass by reference but when I execute following code the strings are not swapped in main method why? static void swap(String s1, String s2){ String temp = s1; s1=s2; s2=temp; } public static void main(String[]…
sabu
  • 1,969
  • 4
  • 18
  • 28
10
votes
2 answers

Pass std algos predicates by reference in C++

I am trying to remove elements from a std::list and keep some stats of deleted elements. In order to do so, I use the remove_if function from the list, and I have a predicate. I would like to use this predicate to gather statistics. Here is the code…
0x26res
  • 11,925
  • 11
  • 54
  • 108
10
votes
4 answers

Does passing by reference always avoid the slicing issue?

This surprised me a little bit, but I was playing around with some code and found out that, at least on my computer, when a function accepts a parent class by reference and you pass a child instance, that the slicing problem doesn't occur. To…
Anthony
  • 9,451
  • 9
  • 45
  • 72
10
votes
6 answers

Setting a ref to a member field in C#

I'd like to assign a reference to a member field. But I obviously do not understand this part of C# very well, because I failed :-) So, here's my code: public class End { public string parameter; public End(ref string parameter) { …
dijxtra
  • 2,681
  • 4
  • 25
  • 37
10
votes
6 answers

Pass List to method without modifying original list

Is this the only way of passing a List to a method and editing that List, without modifying the original List? class CopyTest1 { List _myList = new List(); public CopyTest1(List l) { foreach (int num in l) …
Paligulus
  • 493
  • 1
  • 4
  • 14
10
votes
3 answers

Passing temporaries as non-const references in C++

I have the following piece of code, as an example dec_proxy attempts to reverse the effects of the increment operator upon the type that is executed in a complex function call foo - which btw I cannot change the interface of. #include…
Xander Tulip
  • 1,438
  • 2
  • 17
  • 32