STL and pointer/reference question - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 19 of 19

Thread: STL and pointer/reference question

  1. #16
    Join Date
    Nov 2003
    Location
    Vienna, Austria
    Posts
    212
    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

  2. #17
    Join Date
    Nov 2003
    Posts
    1,405
    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.

  3. #18
    Join Date
    Nov 2003
    Location
    Vienna, Austria
    Posts
    212
    You just proved my point.
    All the buzzt
    CornedBee

  4. #19
    Join Date
    Nov 2003
    Posts
    1,405
    Originally posted by CornedBee
    You just proved my point.
    Good!

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center