pass-by-pointer

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

What do “value semantics’” and “pointer semantics” mean in Go?

て烟熏妆下的殇ゞ 提交于 2019-12-22 08:52:27
问题 What is the meaning of Value semantics and Pointer semantics in Go? In this course, the author used to mention many times about above terms when explaining internals of arrays and slices which I couldn't understand it completely. 回答1: When you call a function or a method and you pass parameters to it, a copy is made from the values, and the function can only access these copies. This means if the function attempts to modify / change the copies, it will not change the original value. For

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

Is using pointers in C++ always bad?

会有一股神秘感。 提交于 2019-12-13 13:15:15
问题 I was told to avoid using pointers in C++. It seems that I can't avoid them however in the code i'm trying to write, or perhaps i'm missing out on other great C++ features. I wish to create a class (class1) which contains another class (class2) as a data member. I then want class2 to know about class1 and be able to communicate with it. I could have a reference to class1 as a member in class2 but that then means I need to provide a reference to class1 as a parameter in the constructor of

Confused by single pointer and double pointer arguments in function calls

蹲街弑〆低调 提交于 2019-12-08 22:59:55
问题 I'm trying to get a deeper understanding on pointer arguments in functions for C. I've written a test program to try to see the difference between passing a single pointer vs a double pointer to a function and then modifying it. I have a program that has two functions. The first function modifyMe1 takes a single pointer as an argument and changes the a property to 7. The second function modifyMe2 takes a double pointer as an argument and changes the a property to 7. I expected that the first

How to pass by reference in Ruby?

妖精的绣舞 提交于 2019-12-08 13:56:05
问题 Currently I'm developing a Watcher class in Ruby, which, among other things, is able to find the period duration of a toggling signal. This is in general rather simple, but a problem I'm facing is that apparently Ruby passes all parameters by value. While researching online I found many different discussions about what "pass by value" and "pass by reference" actually is, but no actual "how to". Coming from a C/C++ background, for me this is an essential part of a programming/scripting

Confused by single pointer and double pointer arguments in function calls

孤街醉人 提交于 2019-11-30 02:31:53
I'm trying to get a deeper understanding on pointer arguments in functions for C. I've written a test program to try to see the difference between passing a single pointer vs a double pointer to a function and then modifying it. I have a program that has two functions. The first function modifyMe1 takes a single pointer as an argument and changes the a property to 7. The second function modifyMe2 takes a double pointer as an argument and changes the a property to 7. I expected that the first function modifyMe1 , would be "pass-by-value" that is if I passed in my struct pointer, C would create

What operators do I have to overload to see all operations when passing an object to a function?

久未见 提交于 2019-11-29 18:44:58
I would like to write a piece of code that shows all copy/assignment/delete etc. operations that are done on the object when passing it to a function. I wrote this: #include <iostream> class A { public: A(){std::cout<<"A()"<<std::endl;} void operator=(const A& a ){std::cout<<"A=A"<<std::endl;} A(const A& a){std::cout<<"A(A)"<<std::endl;} ~A(){std::cout<<"~A"<<std::endl;} }; void pv(A a){std::cout<<"pv(A a)"<<std::endl;} void pr(A& a){std::cout<<"pr(A& a)"<<std::endl;} void pp(A* a){std::cout<<"pp(A* a)"<<std::endl;} void pc(const A& a){std::cout<<"pc(const A& a)"<<std::endl;} int main() { std:

What operators do I have to overload to see all operations when passing an object to a function?

佐手、 提交于 2019-11-28 12:28:24
问题 I would like to write a piece of code that shows all copy/assignment/delete etc. operations that are done on the object when passing it to a function. I wrote this: #include <iostream> class A { public: A(){std::cout<<"A()"<<std::endl;} void operator=(const A& a ){std::cout<<"A=A"<<std::endl;} A(const A& a){std::cout<<"A(A)"<<std::endl;} ~A(){std::cout<<"~A"<<std::endl;} }; void pv(A a){std::cout<<"pv(A a)"<<std::endl;} void pr(A& a){std::cout<<"pr(A& a)"<<std::endl;} void pp(A* a){std::cout<