References vs Pointer vs copying and storing stuff in classes

This is a discussion on References vs Pointer vs copying and storing stuff in classes within the C++ Programming forums, part of the General Programming Boards category; no prizes for guessing what I'm going to suggest.... if you want transfer of ownership semantics without the hassle of ...

  1. #16
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    no prizes for guessing what I'm going to suggest....

    if you want transfer of ownership semantics without the hassle of worrying about deleting the object, use a boost::shared_ptr.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    shared_ptr, as the name says, is about shared ownership, not transferring it. Use std::auto_ptr for that.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #18
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    I always thought that the shared-ownership idiom was one of the possible ways of achieving ownership-transfer. Why use std::auto_ptr if I can transfer ownership through sharing?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #19
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by CornedBee
    shared_ptr, as the name says, is about shared ownership, not transferring it. Use std::auto_ptr for that.
    technically you're right, but I dislike using auto_ptr for anything other than very simple scoped RAII precisely because of it's unintuitive transfer of ownership semantics. You can achieve the same transfer with a shared_ptr, by allowing the 'first' shared_pte to go out of scope
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  5. #20
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    I would probably prefer a combination of unique() and swap() with a null shared_ptr.

    I certainly understand what CornedBee means, but despite the absence of a release() function (which makes sense of course), I believe it's possible to obtain ownership transfer without the need to use auto_ptr. Arguably, shared_ptr is an ugly choice since it makes little sense to expect transfer of ownership when working with ownership sharing, or so I seem to think. But intrusive_ptr, for instance would make a less ugly option.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #21
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    shared_ptr is fine for most things, but I just don't associate the term "transfer of ownership" with it.

    With auto_ptr you signal that your library wants absolutely exclusive use of the object in question and that with passing the object to the library the user's control over it has ended, thank you very much.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21