pass-by-reference

Java is NEVER pass-by-reference, right?…right? [duplicate]

扶醉桌前 提交于 2019-12-17 09:09:14
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Is Java “pass-by-reference”? I found an unusual Java method today: private void addShortenedName(ArrayList<String> voiceSetList, String vsName) { if (null == vsName) vsName = ""; else vsName = vsName.trim(); String shortenedVoiceSetName = vsName.substring(0, Math.min(8, vsName.length())); //SCR10638 - Prevent export of empty rows. if (shortenedVoiceSetName.length() > 0) { if (!voiceSetList.contains("#" +

Is Java really passing objects by value? [duplicate]

耗尽温柔 提交于 2019-12-17 07:10:19
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Is Java pass by reference? public class myClass{ public static void main(String[] args){ myObject obj = new myObject("myName"); changeName(obj); System.out.print(obj.getName()); // This prints "anotherName" } public static void changeName(myObject obj){ obj.setName("anotherName"); } } I know that Java pass by value, but why does it pass obj by reference in previous example and change it? 回答1: Java always passes

'pass parameter by reference' in Ruby?

*爱你&永不变心* 提交于 2019-12-17 03:42:09
问题 In Ruby, is it possible to pass by reference a parameter with value-type semantics (e.g. a Fixnum)? I'm looking for something similar to C#'s ' ref ' keyword. Example: def func(x) x += 1 end a = 5 func(a) #this should be something like func(ref a) puts a #should read '6' Btw. I know I could just use: a = func(a) 回答1: You can accomplish this by explicitly passing in the current binding: def func(x, bdg) eval "#{x} += 1", bdg end a = 5 func(:a, binding) puts a # => 6 回答2: Ruby doesn't support

C++ pass an array by reference

给你一囗甜甜゛ 提交于 2019-12-17 02:42:09
问题 is this allowed to pass an array by reference ? void foo(double& *bar) Seems that my compiler says no. Why? What is the proper way to pass an array by reference? Or a work around? I have an array argument that my method should modify and that I should retrieve afterwards. Alternatively, I could make this array a class member, which works fine, but it has many drawbacks for other part of my code (that I would like to avoid). Thanks and regards. 回答1: Arrays can only be passed by reference,

Passing a String by Reference in Java?

南楼画角 提交于 2019-12-17 02:33:52
问题 I am used to doing the following in C : void main() { String zText = ""; fillString(zText); printf(zText); } void fillString(String zText) { zText += "foo"; } And the output is: foo However, in Java, this does not seem to work. I assume because the String object is copied instead of passed by referenced . I thought Strings were objects, which are always passed by reference. What is going on here? 回答1: You have three options: Use a StringBuilder: StringBuilder zText = new StringBuilder ();

c++ passing by const pointer or reference to const pointer

情到浓时终转凉″ 提交于 2019-12-14 03:49:34
问题 I am learning c++, recently i read a book which gives a suggestion that you should use reference to const when possible (if base object will not be changed). I have a question that should you pass reference to const pointer instead of const pointer, when possible, as reference prevents copied. If not when should i use reference to const pointer. e.g: Node(..., Node *const next = nullptr); OR Node(..., Node* const& next = nullptr); 回答1: Passing by const reference is good practice when passing

Referencing an object array in C# PInvoke

只愿长相守 提交于 2019-12-14 03:36:16
问题 I'm building a spectrometry application which uses a C# GUI and a native C++ logical dll. I'm trying to make the dll fill an array of simple C++ structs passed by reference from the C# side. However, when I try to print the [supposed to be filled] array elements, I'm getting System.NullReferenceExceptions and the array elements are marked as null in memory. Here is the C++ struct definition and method implementation: typedef struct intensitytype { unsigned short usEchelleOrder; // echelle

ArrayList of object references

戏子无情 提交于 2019-12-14 03:35:10
问题 Having a user defined class, like this: class Foo { public int dummy; public Foo(int dummy) { this.dummy = dummy; } } And having then something like this: ArrayList dummyfoo = new ArrayList(); Foo a = new Foo(1); dummyfoo.add(a); foreach (Foo x in dummyfoo) x.dummy++; How much is a.dummy? How can i create my ArrayList so that a.dummy equals 2, meaning that my ArrayList contains basically pointers to my objects and not copies. 回答1: It is already 2, as Array/Collections (to be precise any .NET

Using Pass by value or Pass by Reference?

自古美人都是妖i 提交于 2019-12-14 03:34:43
问题 I have two methods in my code. Below is one of them. private async void Characteristic_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args) { var newValue = FormatValueByPresentation(args.CharacteristicValue, presentationFormat); var message = $"Value at {DateTime.Now:hh:mm:ss.FFF}: {newValue}"; await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => CharacteristicLatestValue.Text = message); } And it's printing time (Value At) like this. Now, this is the second

How to pass by reference a sub-array which is in a dynamically allocated 2D array?

删除回忆录丶 提交于 2019-12-14 00:15:33
问题 I need to pass by reference sub-arrays which are part of a dynamically allocated 2D array. I have tried the following approach, which doesn't seem to work. Any ideas if it is possible? void set1(int *a){ a = malloc(2*sizeof(int)); a[0] = 5; a[1] = 6; } void set2(int *a){ a = malloc(2*sizeof(int)); a[0] = 7; a[1] = 8; } int main(){ int **x = malloc(2*sizeof(int*)); set1(x[0]); set2(x[1]); return 0; } 回答1: You need to pass the address of your sub-arrays, like in this example: void set(int ** a)