Reputation:
I read that:
In Java, all parameters are passed by value. In C++, a parameter can be passed by:
value, reference, or const-reference
However, if I pass a pointer to a function and in that function I change the pointer, this will not change the object, whose identifier was used to specify it as a parameter, it will only change the pointer passed into the function.
That actually means that technically, in C++ everything is PASSED BY VALUE AS A COPY, the difference is that this copied value can be used as a memory address (pointer or reference), so if I want to modify an existing pointer passed as a function parameter I must pass the value of its memory address and not the value of the memory address, held by that pointer.
So is the assumptions that every function parameter is a new copy derived from an existing object created specifically to serve as a parameter in a single particular call and NEVER an actual pre-existing object a correct one?
Upvotes: 5
Views: 387
Reputation: 129464
What is passed to a function is indeed a unique object. However, if we have object X
, and we take make a reference object& Y = X;
, and then object& Z = X;
, then we have three "objects" - X
which is of type object
, and Y
and Z
which are of the type reference to object
. There is still only one "real" instance of object
- Y
and Z
are both referencing the same X
.
Likewise, if we pass a reference to a function:
void func(object& R)
{
...
}
with
func(X);
the function will receive an object that is a reference to X
- which makes the function appear as if it's actually modifying X
, since that is how references work - the actual value of a reference is something that refers to the original object.
What I'm trying to say is that "everything passed into a function is a unique object, but it may be referring to the same original object in case of a reference".
In C++, pointers and references are very similar, aside from two things:
1. A reference can only be initialized to a particular object once - and it can only be created in such a way that it actually refers to an object, you can't have a reference that refers to "nothing" or "isn't set yet" [you can however have "stale" references, where the object that it originally referred to no longer exists].
2. References uses the same syntax as the basic object, e.g. r.member = 42;
, where pointers use *p
or p->member = 42;
to "get" to the content of the object.
[Yes, it's entirely possible, within the C++ specification to define a compiler where pointers and references are "not the same thing internally", but so far I've never seen one]
Upvotes: 0
Reputation: 726839
In Java, all parameters are passed by value.
This is somewhat misleading, because when object references are passed by value, and the objects support mutation, the caller can change the object passed in, effectively making it look like a pass by reference.
That actually means that technically, in C++ everything is PASSED BY VALUE AS A COPY, the difference is that this copied value can be used as a memory address (pointer or reference), so if I want to modify an existing pointer passed as a function parameter I must pass the value of its memory address and not the value of the memory address, held by that pointer.
This assessment is correct. Passing by pointer and passing by reference still creates a copy of something in the space of the function being called, at least logically:
In short, whatever is passed to the caller, is passed by value. Sometimes, that passed value can be used to modify the original object.
When you consider optimization and inlining, things start getting messier: it is possible that no copy of your pointer or the reference is created, but the overall logic remains the same.
Upvotes: 4
Reputation: 14174
That actually means that technically, in C++ everything is PASSED BY VALUE AS A COPY, the difference is that this copied value can be used as a memory address (pointer or reference)
Yes, your assumption technically is right, for almost all common programming languages. Why? You should note that pass by reference is a software abstraction. What the CPU really does (Of course this depends on the function call convention, I'm simplifying things) is to copy the data into registers.
To simulate that you are working with the same value, what you do (And what most common languages do) is to pass the memory address of the original data, in other words, a pointer. Thats what C does to simulate pass by reference, thats what C++ does (References are really pointers), and thats what Java does.
Upvotes: 1