pass-by-reference

R: Pass data.frame by reference to a function

感情迁移 提交于 2019-12-19 18:29:00
问题 I pass a data.frame as parameter to a function that want to alter the data inside: x <- data.frame(value=c(1,2,3,4)) f <- function(d){ for(i in 1:nrow(d)) { if(d$value[i] %% 2 == 0){ d$value[i] <-0 } } print(d) } When I execute f(x) I can see how the data.frame inside gets modified: > f(x) value 1 1 2 0 3 3 4 0 However, the original data.frame I passed is unmodified: > x value 1 1 2 2 3 3 4 4 Usually I have overcame this by returning the modified one: f <- function(d){ for(i in 1:nrow(d)) {

Passing an IDisposable object by reference causes an error?

丶灬走出姿态 提交于 2019-12-19 10:16:09
问题 I am trying to create a general method for disposing an object that implements IDisposable, called DisposeObject() To make sure I am disposing an object pointed by original reference, I am trying to pass an object by reference. But I am getting a compilation error that says The 'ref' argument type doesn't match parameter type In the below (simplified) code, both _Baz and _Bar implement IDisposable. So the questions are, Why am I getting this error? Is there a way to get around it? [UPDATE]

How does Python referencing work?

天涯浪子 提交于 2019-12-19 08:09:07
问题 I am confused with Python referencing. Consider the following example: My task : To edit each element in the list d = { 'm': [1,2,3] } m = d['m'] m = m[1:] # m changes its reference to the new sliced list, edits m but not d (I wanted to change d) Similarly: d = { 'm': [1,2,3] } m = d['m'] m = m[0] # As per python referencing, m should be pointing to d['m'] and should have edited d In python everything goes by reference, then when is a new object created? Do we always need copy and deepcopy

Assigning variables by reference and ternary operator?

冷暖自知 提交于 2019-12-19 07:11:53
问题 Why ternary operator doesn't work with assignment by reference? $obj = new stdClass(); // Object to add $result = true; // Op result $success = array(); // Destination array for success $errors = array(); // Destination array for errors // Working $target = &$success; if(!$result) $target = &errors; array_push($target, $obj); // Not working $target = $result ? &$success : &$errors; array_push($target, $obj); 回答1: Here you go $target = ($result ? &$success : &$errors); Also your example has

Assigning variables by reference and ternary operator?

不打扰是莪最后的温柔 提交于 2019-12-19 07:11:45
问题 Why ternary operator doesn't work with assignment by reference? $obj = new stdClass(); // Object to add $result = true; // Op result $success = array(); // Destination array for success $errors = array(); // Destination array for errors // Working $target = &$success; if(!$result) $target = &errors; array_push($target, $obj); // Not working $target = $result ? &$success : &$errors; array_push($target, $obj); 回答1: Here you go $target = ($result ? &$success : &$errors); Also your example has

Does passing Reference Types using ref save memory?

删除回忆录丶 提交于 2019-12-19 06:21:48
问题 In C#, the parameters to a method can be either reference types or value types. When passing reference types, a copy of the reference is passed. This way, if inside a method we try to reassign the passed reference to another object instance, outside of the method the reassignment is not visible. To make this working, C# has the ref modifier. Passing a reference type with ref actually uses the original reference instead of a copy. (Correct me if I'm wrong). In this case, since we are not

Pass reference to output location vs using return

送分小仙女□ 提交于 2019-12-19 05:49:10
问题 Which is better for performance when calling a function that provides a simple datatype -- having it fill in a memory location (passed by pointer) or having it return the simple data? I've oversimplified the example returning a static value of 5 here, but assume the lookup/functionality that determines the return value would be dynamic in real life... Conventional logic would tell me the first approach is quicker since we are operating by reference instead of having to return a copy as in the

Why can't my Java method change a passed variable? [duplicate]

谁说我不能喝 提交于 2019-12-19 02:28:09
问题 This question already has answers here : Is Java “pass-by-reference” or “pass-by-value”? (86 answers) Closed last year . I was kind of baffled when I saw the following code did not work as expected. I thought Java always passed variables by references into functions. Therefore, why can't the function reassign the variable? public static void main(String[] args) { String nullTest = null; setNotNull(nullTest); System.out.println(nullTest); } private static void setNotNull(String s) { s = "not

Why is function call by reference in PHP deprecated?

风流意气都作罢 提交于 2019-12-18 21:21:32
问题 I wrote the following code: <?php $a1 = "WILLIAM"; $a2 = "henry"; $a3 = "gatES"; echo $a1." ".$a2." ".$a3. "<br />"; fix_names($a1, $a2, $a3); echo $a1." ".$a2." ".$a3; function fix_names(&$n1, &$n2, &$n3) { $a1 = ucfirst(strtolower(&$n1)); $a2 = ucfirst(strtolower(&$n2)); $a3 = ucfirst(strtolower(&$n3)); } ?> I received this notice: Deprecated: Call-time pass-by-reference has been deprecated I need an explanation why did I receive this notice? And why in PHP Version 5.3.13, this has been

Invalid initialization of non-const reference of type

早过忘川 提交于 2019-12-18 18:53:20
问题 In the following code, I'm not able to pass a temporary object as argument to the printAge function: struct Person { int age; Person(int _age): age(_age) {} }; void printAge(Person &person) { cout << "Age: " << person.age << endl; } int main () { Person p(50); printAge(Person(50)); // fails! printAge(p); return 0; } The error I get is: error: invalid initialization of non-const reference of type ‘Person&’ from an rvalue of type ‘Person’ I realize that this is something to do with passing an