pass-by-reference

Can I change String object's value passed to my method?

主宰稳场 提交于 2020-12-29 08:55:01
问题 I found the following question Is Java "pass-by-reference" or "pass-by-value"?. I read almost all of it, but could not find out yet what should I do if I want the foo(-) method, to change my String 's value ? (maybe or not reference too, it doesn't matter to me). void foo(String errorText){ errorText="bla bla"; } int main(){ String error="initial"; foo(error); System.out.println(error); } I want to see bla bla on the console. Is it possible? 回答1: You can't change the value of errorText in foo

Is it possible to modify the reference of an argument in Dart?

孤人 提交于 2020-12-09 09:58:17
问题 Not sure if the terminology in the title is 100% correct, but what I mean is easily illustrated by this example: class MyClass{ String str = ''; MyClass(this.str); } void main() { MyClass obj1 = MyClass('obj1 initial'); print(obj1.str); doSomething(obj1); print(obj1.str); doSomethingElse(obj1); print(obj1.str); } void doSomething(MyClass obj){ obj.str = 'obj1 new string'; } void doSomethingElse(MyClass obj){ obj = MyClass('obj1 new object'); } This will print obj1 initial obj1 new string obj1

Is it possible to modify the reference of an argument in Dart?

徘徊边缘 提交于 2020-12-09 09:54:41
问题 Not sure if the terminology in the title is 100% correct, but what I mean is easily illustrated by this example: class MyClass{ String str = ''; MyClass(this.str); } void main() { MyClass obj1 = MyClass('obj1 initial'); print(obj1.str); doSomething(obj1); print(obj1.str); doSomethingElse(obj1); print(obj1.str); } void doSomething(MyClass obj){ obj.str = 'obj1 new string'; } void doSomethingElse(MyClass obj){ obj = MyClass('obj1 new object'); } This will print obj1 initial obj1 new string obj1

How to pass pointer to function and dynamically allocate memory within function C++

时光怂恿深爱的人放手 提交于 2020-12-05 07:23:53
问题 I'm trying to declare a pointer and pass that pointer to a function where memory is allocated. Here is a minimal example: #include <string> #include <iostream> using namespace std; void alloc_mem(int &size, double *x); int main() { double *X; int imax; alloc_mem(imax, X); cout << "imax = " << imax << endl; for (int i = 0; i < imax; i++) { cout << "X = " << X[i] << endl; } delete[]X; return 0; } void alloc_mem(int &size, double *x) { size = 10; x = new double[size]; for (int i = 0; i < size; i

How to pass pointer to function and dynamically allocate memory within function C++

£可爱£侵袭症+ 提交于 2020-12-05 07:22:20
问题 I'm trying to declare a pointer and pass that pointer to a function where memory is allocated. Here is a minimal example: #include <string> #include <iostream> using namespace std; void alloc_mem(int &size, double *x); int main() { double *X; int imax; alloc_mem(imax, X); cout << "imax = " << imax << endl; for (int i = 0; i < imax; i++) { cout << "X = " << X[i] << endl; } delete[]X; return 0; } void alloc_mem(int &size, double *x) { size = 10; x = new double[size]; for (int i = 0; i < size; i

Classic ASP - passing a property as byref

感情迁移 提交于 2020-11-29 09:25:07
问题 In classic ASP I have an object, call it bob . This then has a property called name , with let and get methods. I have a function as follows: sub append(byref a, b) a = a & b end sub This is simply to make it quicker to add text to a variable. I also have the same for prepend , just it is a = b & a . I know it would be simple to say bob.name = bob.name & "andy" , but I tried using the above functions and neither of them work. The way I am calling it is append bob.name, "andy" . Can anyone see

Classic ASP - passing a property as byref

你离开我真会死。 提交于 2020-11-29 09:24:26
问题 In classic ASP I have an object, call it bob . This then has a property called name , with let and get methods. I have a function as follows: sub append(byref a, b) a = a & b end sub This is simply to make it quicker to add text to a variable. I also have the same for prepend , just it is a = b & a . I know it would be simple to say bob.name = bob.name & "andy" , but I tried using the above functions and neither of them work. The way I am calling it is append bob.name, "andy" . Can anyone see

Is it more conventional to pass-by-value or pass-by-reference when the method needs ownership of the value?

北慕城南 提交于 2020-11-26 13:28:31
问题 When I'm passing a object by reference to a struct's new() method, and the struct will own the object, is it more conventional to: pass the object by reference, and do to_owned() in the new() clone the object before calling new() , and pass by value, moving it I can think of pros and cons of each in terms of clarity and separation-of-concerns. #[derive(Clone)] struct MyState; struct MyStruct { state: MyState, } impl MyStruct { pub fn new_by_ref(state: &MyState) -> Self { MyStruct { state:

Is it more conventional to pass-by-value or pass-by-reference when the method needs ownership of the value?

蹲街弑〆低调 提交于 2020-11-26 13:21:52
问题 When I'm passing a object by reference to a struct's new() method, and the struct will own the object, is it more conventional to: pass the object by reference, and do to_owned() in the new() clone the object before calling new() , and pass by value, moving it I can think of pros and cons of each in terms of clarity and separation-of-concerns. #[derive(Clone)] struct MyState; struct MyStruct { state: MyState, } impl MyStruct { pub fn new_by_ref(state: &MyState) -> Self { MyStruct { state:

Is it more conventional to pass-by-value or pass-by-reference when the method needs ownership of the value?

天涯浪子 提交于 2020-11-26 13:20:40
问题 When I'm passing a object by reference to a struct's new() method, and the struct will own the object, is it more conventional to: pass the object by reference, and do to_owned() in the new() clone the object before calling new() , and pass by value, moving it I can think of pros and cons of each in terms of clarity and separation-of-concerns. #[derive(Clone)] struct MyState; struct MyStruct { state: MyState, } impl MyStruct { pub fn new_by_ref(state: &MyState) -> Self { MyStruct { state: