1

Being a value semantic language, C++ passes all parameters by value. So when I pass pointer to an object, the object is passed by reference, but the pointer itself is passed by value (although I believe the object is not passed at all, only the pointer is, and we access the object inside the function by reference. I think this idea also conform to value semantics, since if I thinks like this, value semantics becomes more meaningful, Nothing is passed except value).

So thinking this way makes sense for pointer, as a pointer to something is itself an object, which has both lvalue and rvalue. While passing a pointer as a parameter, a copy of the rvalue is sent, like any other object (ex. passing an integer).

But what about reference? Theoretically, reference is not any individual object, id does not have any identity, simply an alias to the object it refers to. Common implementation of reference may be on top on pointer, but what about uncommon implementation? Even if implemented in terms of pointer, is it passed by value?

Rakib
  • 6,975
  • 6
  • 24
  • 43
  • A reference is treated like a pointer, I think. The compiled binary will not be different than if you had used a pointer. – sashoalm May 16 '14 at 13:56
  • 1
    There's a lot of confusion here, I believe. – Shoe May 16 '14 at 13:58
  • Most likely an address will be pushed on the stack or put in a register, so yes you can think of pointer and references as if they were themselves being passed by value :) – Drax May 16 '14 at 13:59
  • I've just closed as duplicate. This question is answered in so many other questions already: [1](http://stackoverflow.com/questions/410593/pass-by-reference-value-in-c), [2](http://stackoverflow.com/questions/2308368/pointers-vs-references-vs-a-regular-pass-by-value-c), [3](http://stackoverflow.com/questions/1563553/when-do-you-want-to-use-pointers-vs-values-in-c), [4](http://stackoverflow.com/questions/7058339/c-when-to-use-references-vs-pointers), [5](http://stackoverflow.com/questions/4776010/pass-by-reference-and-value-with-pointers) .. – Shoe May 16 '14 at 14:02
  • @sashoalm, Drax he is talking about *semantics*, where passing references means call-by-reference. That the underlying implementation of the compiler *might* treat references like pointers is not of interest in semantics. The compiler might even inline the code and therefore inserting the called function into the calling environment instead of passing any arguments into the function - you *could* call that "pass inside out", but semantically its still calling a function. – Arne Mertz May 16 '14 at 14:02
  • .. [6](http://stackoverflow.com/questions/10240161/reason-to-pass-a-pointer-by-reference-in-c), [7](http://stackoverflow.com/questions/4174144/default-value-for-a-reference-to-a-pointer), [8](http://stackoverflow.com/questions/6732706/c-passing-arguments-by-reference-and-pointer). – Shoe May 16 '14 at 14:02
  • @Drax in my opinion that's an implementation detail noone should care about. If I declare a local variable x and a reference to x in the next line, why should the compiler waste space to push the address of x on the stack? For all I care a reference is an alias, how it's implemented doesn't concern me at all. – user1942027 May 16 '14 at 14:02
  • @Jefffrey, I guess you should have atleast read carefully before marking as duplicate. – Rakib May 16 '14 at 14:04
  • @RakibulHasan, could you please tell what did I miss? – Shoe May 16 '14 at 14:05
  • @Jefffrey, as Arene pointed out, I am not interested in difference between pass by value/pointer/reference, I wanted to know about pass by reference in terms of value semantics, about what standard says , about the underlying implementation of compiler. – Rakib May 16 '14 at 14:08
  • 3
    If you're interested in the underlying implementation, then it makes no sense to talk about whether C++ references are passed by value, because when you get to the underlying implementation, you're no longer dealing with C++. – Benjamin Lindley May 16 '14 at 14:10
  • @RakibulHasan the problem I see is that the standard doesn't define how things are implemented, it doesn't tell if signed numbers should be implemented as two's complement or one's complement. Hell, it doesn't even require a stack/heap. It just tells how things behave, not how they are implemented. – user1942027 May 16 '14 at 14:13
  • @RakibulHasan, what do you mean by "value semantic"? The linked questions deal with both underlying implementation (which is of no concern to the C++ standard) and standard. – Shoe May 16 '14 at 14:13
  • @ArneMertz & Raphael Miedl, i agree with both of you but i think the OP already gets the fact that it is implementation defined :) – Drax May 16 '14 at 14:14
  • "C++ passes all parameters by value" - are you sure you're not confusing C++ with Java? – Cubbi May 16 '14 at 15:17
  • @Cubbi, when we send any pointer to an object, the object is sent by reference, but the *pointer* itself is sent by value, that's what I meant. Please correct me if wrong. – Rakib May 16 '14 at 15:34
  • @RakibulHasan: That point is correct. He's not referring to pointers, he's referring to reference parameters. When you pass an object into a function which has a reference parameter, the passed object is passed by reference. It's the same point that was made by David in the very first comment. – Benjamin Lindley May 17 '14 at 05:31

0 Answers0