-
December 28th, 2003, 07:12 AM
#16
The references are passed by value, but the objects are passed by reference.
See this thread for a discussion on terminology
http://www.codeguru.com/forum/showth...hreadid=277180
Some objects are constant in that they do not allow their users to modify their state: all the primitive wrappers, String, the additional number classes. Even if they are passed by reference (as they are), you cannot modify them. It's like passing a pointer to an object in C++, where the object pointed to has a private assignment operator, no public properties and no methods that modify any internal values. You can change the pointer, but not the object. Still, the object was passed by reference.
But you can for example pass a StringBuffer to a function and modify it there. The modification will be visible outside the function because the StringBuffer was passed by reference. To pass an object "pseudo-by-value", you have to do something like
Code:
StringBuffer sb = new StrinBuffer();
wantsByValue((StringBuffer)sb.clone());
The other one is to create a new vector on the heap in the function and then return a pointer to it.
Something I don't like at all. I can't stand functions that allocate stuff and return a pointer to it if there is no logical end function that deletes the memory.
All the buzzt
CornedBee
-
December 28th, 2003, 09:52 AM
#17
Originally posted by CornedBee
The references are passed by value, but the objects are passed by reference.
You may say so in everyday speech but technically it's NOT correct. This example is from p. 145 in the C++ book by Stroustrup,
void f(int val, int& ref)
The first argument is passed according to the pass-by-value mechanism, the second is passed using pass-by-reference. It doesn't matter what you pass, the whole difference is in the passing MECHANISM. In the example the arguments are int's but they could as well have been references (pointers to objects). It wouldn't have changed anything.
-
December 28th, 2003, 10:03 AM
#18
You just proved my point.
All the buzzt
CornedBee
-
December 28th, 2003, 12:58 PM
#19
Originally posted by CornedBee
You just proved my point.
Good!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|