pass-by-value

Can we overload a function based on only whether a parameter is a value or a reference?

陌路散爱 提交于 2019-11-30 20:05:11
I got the answer NO! Because passing by value and passing by reference looks identical to the caller. However, the code below compiles right class A { public: void f(int i) {} void f(int& i) {} }; But when I try to use it, there is compile error. int main () { A a; int i = 9; int& j = i; a.f(1); a.f(i); a.f(j); return 0; } Why does not the compiler disable it even without knowing it is going to be used? Yes, they can be overloaded based on reference or not. That is why it's perfectly fine to have them coexist like that; they are different. The problem has to do with ambiguity. While f(1) can

Why can't my Java method change a passed variable? [duplicate]

≯℡__Kan透↙ 提交于 2019-11-30 19:50:07
This question already has an answer here: Is Java “pass-by-reference” or “pass-by-value”? 84 answers I was kind of baffled when I saw the following code did not work as expected. I thought Java always passed variables by references into functions. Therefore, why can't the function reassign the variable? public static void main(String[] args) { String nullTest = null; setNotNull(nullTest); System.out.println(nullTest); } private static void setNotNull(String s) { s = "not null!"; } This program outputs null . References to objects are passed by value in Java so assigning to the local variable

Is Java pass by value Or pass by reference Or both? [duplicate]

為{幸葍}努か 提交于 2019-11-30 19:46:16
问题 This question already has answers here : Is Java “pass-by-reference” or “pass-by-value”? (86 answers) Closed 6 years ago . Consider following case. List<Integer> listOne = new ArrayList<>(); List<Integer> listTwo = new ArrayList<>(); listOne.add(1);I think this happens due to listOne.add(2); listOne.add(3); Collections.reverse(listOne); listTwo = listOne; //listTwo has same reference Collections.reverse(listOne); System.out.println(listOne); //out put [1, 2, 3] System.out.println(listTwo); //

Why are objects automatically passed by reference?

你说的曾经没有我的故事 提交于 2019-11-30 18:55:48
I have a general question about deep- and shallow-copy in the context of the pass-by-reference- and pass-by-value-concept of C#: In C# it is a requirement to explicitly create methods that accept pointers/references to be able to pass such to the method. However, at least objects passed as parameters to methods/constructors are behaving differently from the rest. It seems they are always passed by reference if no extra cloning is done as described here: http://zetcode.com/lang/csharp/oopii/ . Why are objects automatically passed by reference? Is there any particular benefit from forcing the

Can someone explain to me what the reasoning behind passing by “value” and not by “reference” in Java is?

假如想象 提交于 2019-11-30 14:12:43
问题 I'm fairly new to Java (been writing other stuff for many years) and unless I'm missing something (and I'm happy to be wrong here) the following is a fatal flaw... String foo = new String(); thisDoesntWork(foo); System.out.println(foo);//this prints nothing public static void thisDoesntWork(String foo){ foo = "howdy"; } Now, I'm well aware of the (fairly poorly worded) concept that in java everything is passed by "value" and not "reference", but String is an object and has all sorts of bells

Confused, whether java uses call by value or call by reference when an object reference is passed? [duplicate]

橙三吉。 提交于 2019-11-30 13:44:23
问题 This question already has answers here : Is Java “pass-by-reference” or “pass-by-value”? (86 answers) Closed 5 years ago . public class program1{ public static void main(String args[]){ java.util.Vector vc=new java.util.Vector(); vc.add("111"); vc.add("222"); functioncall(vc); vc.add("333"); System.out.println(vc); } public static void functioncall(java.util.Vector vc){ vc=null; } } The output of above program is [111,222,333]. but, when I run the following program the output is [333].

Can someone explain to me what the reasoning behind passing by “value” and not by “reference” in Java is?

旧城冷巷雨未停 提交于 2019-11-30 09:19:13
I'm fairly new to Java (been writing other stuff for many years) and unless I'm missing something (and I'm happy to be wrong here) the following is a fatal flaw... String foo = new String(); thisDoesntWork(foo); System.out.println(foo);//this prints nothing public static void thisDoesntWork(String foo){ foo = "howdy"; } Now, I'm well aware of the (fairly poorly worded) concept that in java everything is passed by "value" and not "reference", but String is an object and has all sorts of bells and whistles, so, one would expect that unlike an int a user would be able to operate on the thing that

Modify an array passed as a method-parameter

99封情书 提交于 2019-11-30 08:43:32
问题 Suppose I have an int-array and I want to modify it. I know that I cannot assign a new array to array passed as parameter: public static void main(String[] args) { int[] temp_array = {1}; method(temp_array); System.out.println(temp_array[0]); // prints 1 } public static void method(int[] n) { n = new int[]{2}; } while I can modify it: public static void main(String[] args) { int[] temp_array = {1}; method(temp_array); System.out.println(temp_array[0]); // prints 2 } public static void method

how do people deal with iterating a Swift struct value-type property?

怎甘沉沦 提交于 2019-11-30 07:02:31
问题 Here's an obvious situation that must arise all the time for people: struct Foundation { var columns : [Column] = [Column(), Column()] } struct Column : CustomStringConvertible { var cards = [Card]() var description : String { return String(describing:self.cards) } } struct Card {} var f = Foundation() for var c in f.columns { c.cards.append(Card()) } That code is legal but of course it has no effect on f , because var c is still a copy — the actual columns of f are unaffected. I am not

Why the second argument to pthread_join() is a **, a pointer to a pointer?

做~自己de王妃 提交于 2019-11-30 03:19:01
问题 I am new to using pthread and also not that familiar with pointers to pointers. Could somebody perhaps explain why the second argument of pthread_join() is a void ** . Why is it designed like this. int pthread_join(pthread_t thread, void **value_ptr); 回答1: To return a value via a function's argument you need to pass in the address of the variable to receive the new value. As pthread_join() is designed to receive the pointer value being passed to pthread_exit(), which is a void* , pthread_join