pass-by-reference

C++ Pass By Const Reference and Return By Const Reference

泄露秘密 提交于 2020-01-11 04:35:10
问题 I'm trying to understand if there is any benefit to returning a const reference. I have a factorial function that normally looks like this: unsigned long factorial(unsigned long n) { return (n == 0) ? 1 : n * factorial(n - 1); } I'm assuming that there will be a performance increase when we pass by const reference and we return a const reference... but const -correctness always confuses me. const unsigned long & factorial(const unsigned long& n) { return (n == 0) ? 1 : n * factorial(n - 1); }

Array seems to be getting passed by reference in Java, how is this possible? [duplicate]

[亡魂溺海] 提交于 2020-01-10 08:22:18
问题 This question already has answers here : Are arrays passed by value or passed by reference in Java? [duplicate] (7 answers) Closed 6 months ago . I can post more code if I need to, but before that I would like to just ask a general question about the following method in which an array is passed, and then set to another array, but for some reason the original array, the one being passed in, is also getting changed, how this possible/what should i do? Thanks tempBoard is an array of same size

How do I pass the value (not the reference) of a JS variable to a function? [duplicate]

自古美人都是妖i 提交于 2020-01-08 09:29:42
问题 This question already has answers here : JavaScript closure inside loops – simple practical example (44 answers) Closed 4 years ago . Here is a simplified version of something I'm trying to run: for (var i = 0; i < results.length; i++) { marker = results[i]; google.maps.event.addListener(marker, 'click', function() { change_selection(i); }); } but I'm finding that every listener uses the value of results.length (the value when the for loop terminates). How can I add listeners such that each

Pass by Reference / Value in C++

点点圈 提交于 2020-01-08 08:44:10
问题 I would like to clarify the differences between by value and by reference. I drew a picture So, for passing by value, a copy of an identical object is created with a different reference, and the local variable is assigned the new reference, so to point to the new copy How to understand the words: " If the function modifies that value, the modifications appear also within the scope of the calling function for both passing by value and by reference " Thanks! 回答1: I think much confusion is

Conflict with SystemParametersInfo signatures

99封情书 提交于 2020-01-06 23:51:44
问题 Simply as this, to run this instruction: NativeMethods.SystemParametersInfo(SPI.SPI_SETKEYBOARDCUES, 0UI, True, SPIF.None) I need this signature: <DllImport("user32.dll")> Private Shared Sub SystemParametersInfo( ByVal uiAction As NativeMethods.SPI, ByVal uiParam As UInteger, ByVal pvParam As Boolean, ByVal fWinIni As NativeMethods.SPIF) End Sub But to run this other instruction: Dim MyBoolean As Boolean = False NativeMethods.SystemParametersInfo(SPI.SPI_GETKEYBOARDCUES, 0UI, MyBoolean, SPIF

Returning a reference to a session variable from the eval() function in PHP

邮差的信 提交于 2020-01-06 15:43:21
问题 Do you know how to return a reference to a $_SESSION variable from the eval() function. class SessionAccessor { static function &getVar() { return eval('return $_SESSION["sample"];'); } } Error checking aside, here is the result I want: $sample =& SessionAccessor::getVar(); $sample = 'new value'; // sets $_SESSION['sample'] to 'new value' If you're wondering whether or not I need to use eval() , the answer is yes. 回答1: class SessionAccessor { static function &getVar($str) { $arr =& $_SESSION;

Passing arguments by pass-by-reference to a swap function

和自甴很熟 提交于 2020-01-06 12:41:34
问题 I encountered this problem while solving a practice test Consider the following code written in a pass-by-reference language like FORTRAN and these statements about the code subroutine swap(ix,iy) it = ix ix = iy ! line L1 iy = it ! line L2 end program main ia = 3 ib = 8 call swap (ia, ib+5) print *, ia, ib end program Statements: The compiler will generate code to allocate a temporary nameless cell, initialize it to 13, and pass the address of the cell swap On execution the code will

Function returning value vs modifying value passed by reference

若如初见. 提交于 2020-01-05 08:25:15
问题 In what situations it is preferred to return an object instead of just modifying an object passed to that function by reference? How do I know which one should I pick? Actually, the question is if there are things I wouldn't be able to do without the ability to return an object from function, but instead only modifying objects passed by reference. 回答1: The main pragmatic difference between TYPE function () ; and void function (TYPE &value) ; is that the former can be used in expressions. a =

why can temporary objects be bound to const reference?

最后都变了- 提交于 2020-01-05 07:18:37
问题 Source of question: The only failing case is passing parameters by non-const reference, since temporary variable couldn't be bound to it. void DrawLine(const Vector& v1, const Vector& v2); If the object is temporary, why would making the reference const have any effect on the lifetime of the temporary object? I guess I also don't fully understand the scope of existence for temporary objects created in an argument. 回答1: If the object is temporary, why would making the reference const have any

data.table reference semantics: memory usage of iterating through all columns

这一生的挚爱 提交于 2020-01-05 03:57:06
问题 When iterating through all columns in an R data.table using reference semantics, what makes more sense from a memory usage standpoint: (1) dt[, (all_cols) := lapply(.SD, my_fun)] or (2) lapply(colnames(dt), function(col) dt[, (col) := my_fun(dt[[col]])])[[1]] My question is: In (2), I am forcing data.table to overwrite dt on a column by column basis, so I would assume to need extra memory on the order of column size. Is this also the case for (1)? Or is all of lapply(.SD, my_fun) evaluated