Timeline for Value of passing by reference [duplicate]
Current License: CC BY-SA 3.0
17 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Jul 10, 2014 at 0:13 | history | closed |
gnat Bart van Ingen Schenau CommunityBot Robert Harvey |
Duplicate of What are the advantages of pass by value? | |
| Jul 9, 2014 at 15:06 | comment | added | Simon B | @wolfPack88: There is a simple way to tell, assuming the programmer of the function has any sense. If the function takes a const reference, then it cannot change the value. If it takes a non-const reference, it may change the value, and you should assume it will. | |
| Jul 9, 2014 at 14:36 | answer | added | utnapistim | timeline score: 2 | |
| Jul 9, 2014 at 13:41 | comment | added | Bart van Ingen Schenau | @wolfPack88: The goal was to allow classes that are a drop-in replacement for the basic types. This means that the user must not need to use different syntax for invoking a built-in or overloaded operator. | |
| Jul 9, 2014 at 13:35 | comment | added | wolfPack88 | @BartvanIngenSchenau: While that makes sense, why not at least have different syntax so it's clear to the client what is and isn't changing? | |
| Jul 9, 2014 at 13:26 | review | Close votes | |||
| Jul 10, 2014 at 0:13 | |||||
| Jul 9, 2014 at 13:24 | comment | added | Bart van Ingen Schenau |
@wolfPack88: Note that pass-by-reference was added to C++ to make operator overloading possible for operators that modify an operand (like operator++).
|
|
| Jul 9, 2014 at 13:13 | comment | added | wolfPack88 | @gnat: The second question helps, thanks. Or rather, the answer to the second question that you linked to. The first one, not as much. | |
| Jul 9, 2014 at 13:05 | comment | added | gbjbaanb | @delnan true, I wasn't trying to provide an exact analogy, but pointing out some things can trip you up no matter what language you use. In C#, that is more often the case with assignments than passing parameters. I should have taken more care with my comment! | |
| Jul 9, 2014 at 13:01 | comment | added | user7043 |
@gbjbaanb A reference type in C# is not like a reference in C++. Local variables, object fields and parameters of class type are more like pointers without pointer arithmetic, meaning the (C#) reference is passed "by value", i.e. copied, but the referenced object is not copied but aliased. In particular, you can write swap(T&, T&) in C++ but you can't write swap(T, T) in C#. For (restricted) C++ style pass-by-reference there's ref (so yes, swap(ref T, ref T) does work).
|
|
| Jul 9, 2014 at 13:01 | history | edited | gnat | CC BY-SA 3.0 |
formatting kaizen
|
| Jul 9, 2014 at 12:56 | comment | added | Blrfl | The only real danger is in calling a function and not understanding what it's going to do with the arguments. | |
| Jul 9, 2014 at 12:51 | comment | added | gbjbaanb | no more so than C# - compare passing a struct to a class. One is a value type, the other a reference so you change the data in one, you change the data of a copy in the other. Other languages have similar issues, you just have to be careful, take your time, and know what you're doing. | |
| Jul 9, 2014 at 12:49 | comment | added | Doval | If the function takes a const reference, it can't change the input. But that seems to be neither here nor there; how will you know the function won't have other side effects? If this sort of thing bothers you, C++ is probably not the language for you. | |
| Jul 9, 2014 at 12:49 | answer | added | Kilian Foth | timeline score: 0 | |
| Jul 9, 2014 at 12:49 | history | edited | wolfPack88 | CC BY-SA 3.0 |
Grammar
|
| Jul 9, 2014 at 12:40 | history | asked | wolfPack88 | CC BY-SA 3.0 |