pass-by-value

C# pass by value vs. pass by reference

非 Y 不嫁゛ 提交于 2019-11-26 09:48:56
问题 Consider the following code (I have purposefully written MyPoint to be a reference type for this example) public class MyPoint { public int x; public int y; } It is universally acknowledged (in C# at least) that when you pass by reference, the method contains a reference to the object being manipulated, whereas when you pass by value, the method copies the value being manipulated, thus the value in global scope is not affected. Example: void Replace<T>(T a, T b) { a = b; } int a = 1; int b =

Emulating pass-by-value behaviour in python

▼魔方 西西 提交于 2019-11-26 09:37:03
问题 I would like to emulate the pass-by-value behaviour in python. In other words, I would like to make absolutely sure that the function I write do not modify user supplied data. One possible way is to use deep copy: from copy import deepcopy def f(data): data = deepcopy(data) #do stuff is there more efficient or more pythonic way to achieve this goal, making as few assumptions as possible about the object being passed (such as .clone() method) Edit I\'m aware that technically everything in

Python : When is a variable passed by reference and when by value? [duplicate]

和自甴很熟 提交于 2019-11-26 09:21:42
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Python: How do I pass a variable by reference? My code : locs = [ [1], [2] ] for loc in locs: loc = [] print locs # prints => [ [1], [2] ] Why is loc not reference of elements of locs ? Python : Everything is passed as reference unless explicitly copied [ Is this not True ? ] Please explain.. how does python decides referencing and copying ? Update : How to do ? def compute(ob): if isinstance(ob,list): return

Is the pass-by-value-and-then-move construct a bad idiom?

泪湿孤枕 提交于 2019-11-26 09:16:51
问题 Since we have move semantics in C++, nowadays it is usual to do void set_a(A a) { _a = std::move(a); } The reasoning is that if a is an rvalue, the copy will be elided and there will be just one move. But what happens if a is an lvalue? It seems there will be a copy construction and then a move assignment (assuming A has a proper move assignment operator). Move assignments can be costly if the object has too many member variables. On the other hand, if we do void set_a(const A& a) { _a = a; }

Const correctness for value parameters

北慕城南 提交于 2019-11-26 09:09:59
问题 I know there are few question about const correctness where it is stated that the declaration of a function and its definition do not need to agree for value parameters. This is because the constness of a value parameter only matters inside the function. This is fine: // header int func(int i); // cpp int func(const int i) { return i; } Is doing this really a best practice? Because I\'ve never seen anyone do it. I\'ve seen this quotation (not sure of the source) in other places this has been

C++ - passing references to std::shared_ptr or boost::shared_ptr

有些话、适合烂在心里 提交于 2019-11-26 07:53:43
问题 If I have a function that needs to work with a shared_ptr , wouldn\'t it be more efficient to pass it a reference to it (so to avoid copying the shared_ptr object)? What are the possible bad side effects? I envision two possible cases: 1) inside the function a copy is made of the argument, like in ClassA::take_copy_of_sp(boost::shared_ptr<foo> &sp) { ... m_sp_member=sp; //This will copy the object, incrementing refcount ... } 2) inside the function the argument is only used, like in Class:

Where should I prefer pass-by-reference or pass-by-value?

China☆狼群 提交于 2019-11-26 07:37:08
问题 In what circumstances should I prefer pass-by-reference? Pass-by-value? 回答1: There are five main cases where you should use pass-by-reference over pass-by-value: If you are calling a function that needs to modify its arguments, use pass-by-reference as its the only way to get this effect (I treat pass-by-reference and pass-by-pointer interchangeably in this case, though with pass-by-pointer you often have to explicitly check for NULL). If you're calling a function that needs to take a large

Passing values in Python [duplicate]

為{幸葍}努か 提交于 2019-11-26 06:25:37
This question already has an answer here: How do I pass a variable by reference? 26 answers When you pass a collection like list, array to another function in python, does it make a copy of it, or is it just a pointer? Python passes references-to-objects by value . Python passes references-to-objects by value (like Java), and everything in Python is an object. This sounds simple, but then you will notice that some data types seem to exhibit pass-by-value characteristics, while others seem to act like pass-by-reference... what's the deal? It is important to understand mutable and immutable

Passing an integer by reference in Python

一个人想着一个人 提交于 2019-11-26 04:58:54
How can I pass an integer by reference in Python? I want to modify the value of a variable that I am passing to the function. I have read that everything in Python is pass by value, but there has to be an easy trick. For example, in Java you could pass the reference types of Integer , Long , etc. How can I pass an integer into a function by reference? What are the best practices? mgilson It doesn't quite work that way in Python. Python passes references to objects. Inside your function you have an object -- You're free to mutate that object (if possible). However, integers are immutable . One

Pass an array to a function by value

你离开我真会死。 提交于 2019-11-26 04:18:11
问题 Below is a snippet from the book C Programming Just the FAQs . Isn\'t this wrong as Arrays can never be passed by reference? VIII.6: How can you pass an array to a function by value? Answer: An array can be passed to a function by value by declaring in the called function the array name with square brackets ( [ and ] ) attached to the end. When calling the function, simply pass the address of the array (that is, the array’s name) to the called function. For instance, the following program