5 ms·
The point of the article is not to talk about low-level specifics of some language, apart, from, maybe, Lisp sometimes. But, surely, not C++ (although, if you c
by vseloved 7y ago
The point of the article is not to talk about low-level specifics of some language, apart, from, maybe, Lisp sometimes. But, surely, not C++ (although, if you could provide a link with a good and accessible explanation of the differences of the 2 styles I'd add it as a reference for further reading). Stil, I don't see that fundamental difference that you mention. If the differences originate from the points that you listed in the original comments can you, please, make the link more explicit? I understand pass-by-reference as being a slightly higher-level and safer C++ alternative to C's direct pointer-based style. But, surely, I may be missing something. But, it seems that the people who answered the SO question I linked are also missing it. For example, here's a quote: "Basically, references and pointers are not very different from an implementation point of view, the main (and very important) difference is in the philosophy: a reference is the object itself, just with a different name."
- vseloved 7y agoAnd here's another quote from this very much upvoted answer (https://stackoverflow.com/a/596750/977052 https://stackoverflow.com/a/596750/977052): "A reference can be thought of as a constant pointer (not to be confused with a pointer to a constant value!) with automatic indirection, ie the compiler will apply the * operator for you."
- pjmlp 7y agoHere is pass by reference and pass by pointer in Pascal. type Point = record x, y : integer end; procedure ByPointer(p : ^Point); begin p^.x := 12 end; procedure ByReference(var p : Point); begin p.x := 12; p.y := 33 end; procedure example var p : Point begin p.x := 23; p.y := 28; WriteLn('The point is ', p.x, ' ', p.y); ByPointer(@p) WriteLn('The point is ', p.x, ' ', p.y); p.x := 23; p.y := 28; WriteLn('The point is ', p.x, ' ', p.y); ByReference(p) WriteLn('The point is ', p.x, ' ', p.y); end; Actually the concept is already exposed in Algol, a couple of years before C was even an idea.
- vseloved 7y agogood point, so, here, it is exactly syntax sugar as, unlike in C++, nothing else except syntax is different from the pointer-based version. Not to mention that, in the article, I was mentioning the general concept that has one (and single) variant, in C and Java, and 2 in Pascal or C++
- pjmlp 7y agoActually it is more than syntax sugar. I should have provided a better example, this is impossible to do languages like Java and in C requires explicit double indirection to fake how reference types work. type Point = record x, y : integer end; procedure ByPointer(p : ^Point) begin New(p); p.x := 12 end; procedure ByReference(var p : ^Point) begin if p <> nil then Dispose(p) end; New(p); p^.x := 12; p^.y := 33 end; procedure example var p : ^Point begin New(p); p^.x := 23; p^.y := 28; WriteLn('The point is ', p^.x, ' ', p^.y, ' at ', p); ByPointer(p) WriteLn('The point is ', p^.x, ' ', p^.y, ' at ', p); p^.x := 23; p^.y := 28; WriteLn('The point is ', p^.x, ' ', p^.y, ' at ', p); ByReference(p); WriteLn('The point is ', p^.x, ' ', p^.y, ' at ', p); (* here p will point to the one allocated in ByReference *) Dispose(p); end;
- jstimpfle 7y ago> Actually it is more than syntax sugar. Where? I see only syntax sugar. The catchy phrase from the Google C++ style guide is stuck in my head: references have value syntax but pointer semantics. And that's it (and that's why I think references are mostly a bad idea). > C requires explicit double indirection Your 2nd example is no different, it's still just "indirection" (value syntax but pointer semantics!). If what you are indirecting is itself a pointer type (as in your 2nd example), then yes, there technically is a "double indirection", but that's besides the point. > impossible to do in languages like Java actually, "in every memory safe language there is". You can do this in Pascal because Pascal is really just C-style systems programming. With a few features like references and ARC that pretend otherwise but quickly fall flat on their head.