|
Boost Users : |
From: GoochRules! (goochrules_at_[hidden])
Date: 2004-02-25 16:09:16
On Feb 25, 2004, at 12:27 PM, voodoo4 wrote:
> I downloaded yesterday the boost libraries (mainly for the memory
> libs) and
> to tell the truth i'm impressed with pool and smart pointers.
> Especially the boost smart pointers will save me from months of coding!
> However i have a question about smart pointers.
>
> Let's say i have these objects(the code is just an example):
>
> class Slave
> {
> Slave();
> ~Slave();
> };
>
> class Master
> {
> Master();
> Master(int size);
> ~Master();
>
> Slave *sl;
> };
>
> I want "sl" to be an array of Slaves on the free store but the sized
> will be
> determined in the constructor.
>
> With normal pointers i would write:
>
> Master::Master(int val)
> {
> sl=new Slave[val];
> }
>
> Master::~Master()
> {
> delete sl;
> }
>
>
> How can i achieve the same with boost smart pointers?
I have no idea (see below).
> I've read the manual three or four times but i still don't get how to
> work
> this out.
> Does this have something to do with the empty ptr?
> I believe solution is in front of my eyes but i'm too blind(as usual)
> to see
> it...
>
I don't think you're using the right tool for the job. From what I
know, smart pointers should be used when you have one object, here you
(potentially) have many. I think what you really want to do here is to
use a container class like stl::list or stl::vector to contain your
slave objects.
If you still want to use smart pointers, I guess you could use a list
of smart pointers, but there's really no point for that if your slave
class is properly coded (for instance, has a ctor, copy ctor, virtual
dtor, and operator=).
--MP
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net