Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #11924

Re: Is this bad? (How can this be avoided?)

From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c++
Subject Re: Is this bad? (How can this be avoided?)
Date 2011-11-29 14:28 +1300
Message-ID <9jiqqgF4d0U28@mid.individual.net> (permalink)
References <52997172-c9ca-4e1b-aad0-98fc277268a8@g1g2000pri.googlegroups.com> <iP-dnQr9WMmhlUnTnZ2dnUVZ8qmdnZ2d@giganews.com> <d93699f8-e11f-4685-8e72-5053a29e71f4@h37g2000pri.googlegroups.com> <9jilrvF4d0U26@mid.individual.net> <b679461b-efc3-4bda-ba5f-a68d2aaaed7c@v31g2000prg.googlegroups.com>

Show all headers | View raw


On 11/29/11 02:17 PM, mike3 wrote:
> On Nov 28, 5:04 pm, Ian Collins<ian-n...@hotmail.com>  wrote:
>> On 11/29/11 12:16 PM, mike3 wrote:
>>
>>
>>
>>> On Nov 28, 3:16 pm, Leigh Johnston<le...@i42.co.uk>    wrote:
>>>> On 28/11/2011 21:44, mike3 wrote:
>>
>>>> <snip code>
>>
>>>>> What to do? Now one could, I suppose, change to Circle* circle = new
>>>>> Circle(35.0, 55.0, 25.0);, but then we need something to take care of
>>>>> the garbage. Adding a delete() in, say, the destructor for Drawing
>>>>> might work, but we make some assumptions that may or may not be true,
>>>>> i.e. one can break it by misusing it, e.g. if one does not use a new
>>>>> to get the parameter or one forgets that destroying drawing also
>>>>> destroys circle, etc. Is it a bad idea to rely on the user's
>>>>> understanding -- to me it doesn't seem so. What should be done?
>>
>>>> Easy: use std::vector<std::tr1::shared_ptr<Shape>    >    (pre C++11) or
>>>> std::vector<std::unique_ptr<Shape>    >    (C++11) instead or take a look at
>>>> the Boost "ptr" containers.
>>
>>> And should the function addShape() be upgraded to use shared_ptr?
>>
>>> And also, though this solves the "garbage collection" issue, it still
>>> seems
>>> the user needs to understand that they must pass an object that's on
>>> the heap,
>>> and that it will be lost when the Drawing object is gone. Is that OK?
>>
>> Most drawing libraries I've used work this way, the container "owns" the
>> drawable objects.  The simplest solution is to have the Drawing
>> destructor free all of the Shape objects it owns.  So they all assume
>> the drawable objects are on the heap.
>>
>> If you want something more complex, such as the same shape in more than
>> one drawing, then something like a shared_ptr is the solution.
>>
>
> So then the requirement of the user to have to explicitly heap
> allocate
> otherwise bad things could happen is okay?

It's certainly not uncommon.  See the Qt documentation for an example.

-- 
Ian Collins

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Is this bad? (How can this be avoided?) mike3 <mike4ty4@yahoo.com> - 2011-11-28 13:44 -0800
  Re: Is this bad? (How can this be avoided?) Leigh Johnston <leigh@i42.co.uk> - 2011-11-28 22:16 +0000
    Re: Is this bad? (How can this be avoided?) mike3 <mike4ty4@yahoo.com> - 2011-11-28 15:16 -0800
      Re: Is this bad? (How can this be avoided?) Ian Collins <ian-news@hotmail.com> - 2011-11-29 13:04 +1300
        Re: Is this bad? (How can this be avoided?) mike3 <mike4ty4@yahoo.com> - 2011-11-28 17:17 -0800
          Re: Is this bad? (How can this be avoided?) Ian Collins <ian-news@hotmail.com> - 2011-11-29 14:28 +1300
      Re: Is this bad? (How can this be avoided?) "MikeWhy" <boat042-nospam@yahoo.com> - 2011-11-28 19:17 -0600
      Re: Is this bad? (How can this be avoided?) Leigh Johnston <leigh@i42.co.uk> - 2011-11-29 14:12 +0000
  Re: Is this bad? (How can this be avoided?) Goran <goran.pusic@gmail.com> - 2011-11-29 00:06 -0800
  Re: Is this bad? (How can this be avoided?) jgk@panix.com (Joe keane) - 2011-11-30 17:43 +0000
    Re: Is this bad? (How can this be avoided?) "MikeWhy" <boat042-nospam@yahoo.com> - 2011-11-30 12:26 -0600
      Re: Is this bad? (How can this be avoided?) jgk@panix.com (Joe keane) - 2011-11-30 20:36 +0000
    Re: Is this bad? (How can this be avoided?) Leigh Johnston <leigh@i42.co.uk> - 2011-11-30 18:26 +0000

csiph-web