Boost logo

Boost Users :

From: cyril.godart_at_[hidden]
Date: 2004-02-26 04:12:35


Hi,

I suggest the following code:

class Master
{
      typedef boost::smart_ptr<Slave> slave_ptr_t;
      typedef boost::smart_ptr<const Slave> slave_cst_ptr_t;

      typedef std::vector<slave_ptr_t> vec_slaves_t;
      typedef std::vector<slave_cst_ptr_t> vec_cst_slaves_t;//defining this
one is good practice as you'll see later

      Master();
      Master(size_t i)
      {
            slaves_.resize(i);// assume slave has an empty ctor defined.
      }

      vec_cst_slaves_t slaves() //something like const vec_cst_slaves_t&
slaves() const should work
      {
          return slaves_; //nice casting that I failed to notice until
someone in this list mentioned it to me
      };

      vec_slaves_t slaves_;
};

As you rightly guessed, a big advantage of this approach is that the
plumbing of copy-assigment
dtor and so on reduces drastically. I find it extremely convenient, even
though there is a performance
hit.

Regards,

Cyril Godart.

Internet
goochrules_at_[hidden]@lists.boost.org - 25/02/2004 21:09

Please respond to boost-users_at_[hidden]

Sent by: boost-users-bounces_at_[hidden]

To: boost-users

cc:

Subject: Re: [Boost-users] shared_ptr/shared_array Question

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 mailing list
Boost-users_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users

This message and any attachments (the "message") is
intended solely for the addressees and is confidential.
If you receive this message in error, please delete it and
immediately notify the sender. Any use not in accord with
its purpose, any dissemination or disclosure, either whole
or partial, is prohibited except formal approval. The internet
can not guarantee the integrity of this message.
BNP PARIBAS (and its subsidiaries) shall (will) not
therefore be liable for the message if modified.

                ---------------------------------------------

Ce message et toutes les pieces jointes (ci-apres le
"message") sont etablis a l'intention exclusive de ses
destinataires et sont confidentiels. Si vous recevez ce
message par erreur, merci de le detruire et d'en avertir
immediatement l'expediteur. Toute utilisation de ce
message non conforme a sa destination, toute diffusion
ou toute publication, totale ou partielle, est interdite, sauf
autorisation expresse. L'internet ne permettant pas
d'assurer l'integrite de ce message, BNP PARIBAS (et ses
filiales) decline(nt) toute responsabilite au titre de ce
message, dans l'hypothese ou il aurait ete modifie.


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