pass-by-value

Passing objects by reference vs value

风流意气都作罢 提交于 2019-11-27 14:07:37
I just want to check my understanding of C#'s ways of handling things, before I delve too deeply into designing my classes. My current understanding is that: Struct is a value type, meaning it actually contains the data members defined within. Class is a reference type, meaning it contains references to the data members defined within. A method signature passes parameters by value , which means a copy of the value is passed to the inside of the method, making it expensive for large arrays and data structures. A method signature that defines a parameter with the ref or out keywords will instead

C++ view types: pass by const& or by value?

ぐ巨炮叔叔 提交于 2019-11-27 13:27:14
问题 This came up in a code review discussion recently, but without a satisfactory conclusion. The types in question are analogues to the C++ string_view TS. They are simple non-owning wrappers around a pointer and a length, decorated with some custom functions: #include <cstddef> class foo_view { public: foo_view(const char* data, std::size_t len) : _data(data) , _len(len) { } // member functions related to viewing the 'foo' pointed to by '_data'. private: const char* _data; std::size_t _len; };

pass by reference and value with pointers [duplicate]

给你一囗甜甜゛ 提交于 2019-11-27 12:53:57
This question already has an answer here: Is passing pointer argument, pass by value in C++? 5 answers I don't understand why passing a pointer to a function doesn't change the data being passed in. If the function proto looked like this: void func( int *p ); and func allocated memory to p, why can't it be used outside of the function? I thought that pointers were addresses? Whilst something like this does what you expect: void func(int *p) { *p = 1; } int a = 2; func(&a); // a is now 1 this does not void func(int *p) { p = new int; } int *p = NULL; func(p); // p is still NULL In both cases,

Are structs 'pass-by-value'?

倖福魔咒の 提交于 2019-11-27 11:40:55
I've recently tried to create a property for a Vector2 field, just to realize that it doesn't work as intended. public Vector2 Position { get; set; } this prevents me from changing the values of its members ( X & Y ) Looking up information on this, I read that creating a property to a Vector2 struct returns only a copy of the original object and not a reference. As a Java developer this confuses me. When are objects in C# passed by value and when are they passed by reference? Are all struct objects passed by value? It is important to realise that everything in C# is passed by value , unless

Is Java really passing objects by value? [duplicate]

老子叫甜甜 提交于 2019-11-27 11:08:26
Possible Duplicate: Is Java pass by reference? public class myClass{ public static void main(String[] args){ myObject obj = new myObject("myName"); changeName(obj); System.out.print(obj.getName()); // This prints "anotherName" } public static void changeName(myObject obj){ obj.setName("anotherName"); } } I know that Java pass by value, but why does it pass obj by reference in previous example and change it? Eng.Fouad Java always passes arguments by value, NOT by reference. In your example, you are still passing obj by its value, not the reference itself. Inside your method changeName , you are

What exactly is the difference between “pass by reference” in C and in C++?

一笑奈何 提交于 2019-11-27 10:32:20
The phrase "pass by reference" is used by C and C++ developers alike but they appear to be used to mean different things. What exactly is the difference between this equivocal phrase in each language? Joseph Mansfield There are questions that already deal with the difference between passing by reference and passing by value . In essence, passing an argument by value to a function means that the function will have its own copy of the argument - its value is copied. Modifying that copy will not modify the original object. However, when passing by reference, the parameter inside the function

change pointer passed by value

怎甘沉沦 提交于 2019-11-27 09:21:16
I have given a function foo(struct node *n) where n is the head node in a linked list. Now foo should change n s.t. it points to the end of the list. But is this possible with this function signature? Assuming t is the pointer to the end of the list: n = t won't work because the pointer is passed by value. *n = *t won't work because I would overwrite the head of the list. Did I miss something? You would need to use a pointer to a pointer: foo(struct node **n) To change what n points to, you do: *n = t; No, because you have a copy of the value of the pointer that's been passed in. You have no

Java is NEVER pass-by-reference, right?…right? [duplicate]

烂漫一生 提交于 2019-11-27 07:23:54
Possible Duplicate: Is Java “pass-by-reference”? I found an unusual Java method today: private void addShortenedName(ArrayList<String> voiceSetList, String vsName) { if (null == vsName) vsName = ""; else vsName = vsName.trim(); String shortenedVoiceSetName = vsName.substring(0, Math.min(8, vsName.length())); //SCR10638 - Prevent export of empty rows. if (shortenedVoiceSetName.length() > 0) { if (!voiceSetList.contains("#" + shortenedVoiceSetName)) voiceSetList.add("#" + shortenedVoiceSetName); } } According to everything I've read about Java's behavior for passing variables, complex objects or

Is There a Reason Standard Algorithms Take Lambdas by Value? [duplicate]

拟墨画扇 提交于 2019-11-27 06:48:20
问题 This question already has an answer here: Why the sequence-operation algorithms predicates are passed by copy? 3 answers So I asked a question here: Lambda Works on Latest Visual Studio, but Doesn't Work Elsewhere to which I got the response, that my code was implementation defined since the standard's 25.1 [algorithms.general] 10 says: Unless otherwise specified, algorithms that take function objects as arguments are permitted to copy those function objects freely. Programmers for whom

template pass by value or const reference or…?

大憨熊 提交于 2019-11-27 03:30:30
问题 I can write a templated function this way template<class T> void f(T x) {...} or this way template<class T> void f(T const& x) {...} I guess that the second option can be more optimal as it explicitly avoids a copy, but I suspect that it can also fail for some specific types T (eg functors?). So, when should use the first option, and when to use the second? There are also this boost::call_traits<T>::param_type and boost::reference_wrapper that were in the answers to my previous question, but