pass-by-value

Function Overloading Based on Value vs. Const Reference

泄露秘密 提交于 2019-12-27 17:40:52
问题 Does declaring something like the following void foo(int x) { std::cout << "foo(int)" << std::endl; } void foo(const int &x) { std::cout << "foo(const int &)" << std::endl; } ever make sense? How would the caller be able to differentiate between them? I've tried foo(9); // Compiler complains ambiguous call. int x = 9; foo(x); // Also ambiguous. const int &y = x; foo(y); // Also ambiguous. 回答1: The intent seems to be to differenciate between invocations with temporaries (i.e. 9 ) and 'regular'

Passing an object by const reference?

主宰稳场 提交于 2019-12-24 20:36:43
问题 In C++ when you want a function to be able to read from an object, but not modify it, you pass a const reference to the function. What is the equivalent way of doing this in php? I know objects in php5 are passed by reference by default, but for readability I think I will continue to use the ampersand before the variable name, like this: function foo(&$obj) { } 回答1: If you want to pass an object but not by reference you can clone the object beforehand. <?php function changeObject($obj) { $obj

How are quantities referenced in Fortran?

一世执手 提交于 2019-12-24 09:48:50
问题 I was told a long time ago that in FORTRAN, everything is passed by value. Therefore I would need to do this (provided mySubroutine is suitably defined elsewhere): double precision :: myArray(2) myArray(1:2) = (/ 2.3d0, 1.5d0 /) CALL mySubroutine(myArray) However, I also found that the program compiles and runs as expected if I do this CALL mySubroutine((/ 2.3d0, 1.5d0 /)) without needing to define an intermediary array myArray . I thought that I was passing myArray into mySubroutine by

How to copy elements from an ArrayList to another one NOT by reference?

谁都会走 提交于 2019-12-24 04:23:25
问题 I'm trying to copy each element from one ArrayList (av) to another one (copia). The thing is that they're copied by reference, so whenever I make any changes in the original one, the copy is modified as well. Of course, this behavior is not wanted. How should I write this method? public void copiarArrayList(ArrayList<Articulo_Venta> copia, ArrayList<Articulo_Venta> av){ copia.clear(); for (int i = 0; i < av.size(); i++) { copia.add(av.get(i)); } } Articulo_Venta has these fields: int codigo;

How do I pass lots of variables to and from a function in Python?

拥有回忆 提交于 2019-12-23 10:14:39
问题 I do scientific programming, and often want to show users prompts and variable pairs, let them edit the variables, and then do the calulations with the new variables. I do this so often, that I wrote a wxPython class to move this code out of the main program. You set up a list for each variable with the type of the variable (string, float, int), the prompt, and the variable's current value. You then place all of these lists in one big list, and my utility creates a neatly formated wxPython

returning value vs pointer in Go constructor

会有一股神秘感。 提交于 2019-12-23 07:16:35
问题 When building a simple object in go, what's the difference between these alternatives? func NewGender(value string) Gender { return Gender{strings.TrimSpace(value)} } func NewGender(value string) *Gender { return &Gender{strings.TrimSpace(value)} } 回答1: The question is a really broad one, and it depends heavily on the rest of your API. So here are just some things you might need to consider when choosing one over another (in no particular order): Unnecessary pointers lead to more work for the

returning value vs pointer in Go constructor

走远了吗. 提交于 2019-12-23 07:14:21
问题 When building a simple object in go, what's the difference between these alternatives? func NewGender(value string) Gender { return Gender{strings.TrimSpace(value)} } func NewGender(value string) *Gender { return &Gender{strings.TrimSpace(value)} } 回答1: The question is a really broad one, and it depends heavily on the rest of your API. So here are just some things you might need to consider when choosing one over another (in no particular order): Unnecessary pointers lead to more work for the

How to add an array to a list by value not by reference?

巧了我就是萌 提交于 2019-12-23 07:00:26
问题 Is there a way to add an array to a list of arrays by value and not by reference? Example: The following prints out "6, 7, 8, 9, 10". I want it to write out "1, 2, 3, 4, 5". int[] testArray = new int[5] { 1, 2, 3, 4, 5 }; List<int[]> testList = new List<int[]>(); testList.Add(testArray); testArray[0] = 6; testArray[1] = 7; testArray[2] = 8; testArray[3] = 9; testArray[4] = 10; foreach(int[] array in testList) { Console.WriteLine("{0}, {1}, {2}, {3}, {4}", array[0], array[1], array[2], array[3

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

Operator overloading by value results in use of moved value

流过昼夜 提交于 2019-12-22 04:42:53
问题 Compiling the following Rust code that uses operator overloading use std::ops::{Add}; #[derive(Show)] struct Point { x: int, y: int } impl Add for Point { type Output = Point; fn add(self, other: Point) -> Point { Point {x: self.x + other.x, y: self.y + other.y} } } fn main() { let p: Point = Point {x: 1, y: 0}; let pp = p + p; } Results in compiler errors due to ownership of p: <anon>:21:18: 21:19 error: use of moved value: `p` <anon>:21 let pp = p + p; ^ <anon>:21:14: 21:15 note: `p` moved