Boost logo

Boost :

From: Rob Stewart (stewart_at_[hidden])
Date: 2005-06-22 08:24:17


From: H <h.v.niekerk_at_[hidden]>
>
> > Let's see if I can recast your problem to something a little
> > simpler to discuss:
> >
> > void function2(some_type data) // stores stuff in caller's object
> > {
> > read_something_from_somewhere(data);
> > }
> >
> > void function1(some_type data) // stores stuff in caller's object
> > {
> > function2(data);
> > }
> >
> > void function3(some_type data)
> > {
> > // do something useful with data
> > }
> >
> > int main()
> > {
> > some_type data; // some pointer type
> > function1(data); // stores stuff in data
> > function3(data);
> > }
> >
> > Is that a good approximation of what you want?
>
> Yes, that looks like it.
>
> > If so, some_type should be boost::shared_ptr<something> and the
> > rest should just work. Of course, you can avoid using
> > boost::shared_ptr if function1() and function2() take non-const
> > references to some_type:
> >
> > void function2(some_type & data) // stores stuff in caller's object
> > {
> > read_something_from_somewhere(data);
> > }
> >
> > void function1(some_type & data) // stores stuff in caller's object
> > {
> > function2(data);
> > }
> >
> > void function3(some_type const & data)
> > {
> > // do something useful with data
> > }
> >
> > int main()
> > {
> > some_type data; // not a pointer type
> > function1(data); // stores stuff in data
> > function3(data);
> > }
> >
> OK. The original way was:
>
> auto_ptr<FtInterface> fti (ftGetIfFromFactory(device, connection));
>
> With shared_ptr would it become like this:
>
> void function2(boost::shared_ptr fti (ftGetIfFromFactory(device,
> connection))
> {
> ...
> fti->writeAndReadAllData(out,inp);
> }

Absolutely not. I'm not sure where you got the idea that you
should somehow get the object in the formal parameter list, but
that's for the object passed from the caller.

> void function1(boost::shared_ptr fti (ftGetIfFromFactory(device,
> connection))
> {
> function2(data);
> }
>
> int main()
> {
> boost::shared_ptr<FtInterface> fti (ftGetIfFromFactory(device,
> connection));
> function1(fti (ftGetIfFromFactory(device, connection));
> }

That should be something like this:

   void function1(boost::shared_ptr<FtInterface> fti)
   {
      function2(fti);
   }

   int main()
   {
      boost::shared_ptr<FtInterface> fti(ftGetIfFromFactory(...));
      function1(fti);
   }

Do you see that "boost::shared_ptr<FtInterface>" appears
everwhere I wrote "some_type" previously in this version and not
in yours?

> What would be the benefit of using non-const references?

I told you that already. You don't need pointers (since you're
using a factory function to create the instance, you'll need one
pointer, but the other uses don't have to be pointers).

   void function1(FtInterface & fti)
   {
      function2(fti);
   }

   int main()
   {
      std::auto_ptr<FtInterface> fti(ftGetIfFromFactor(...));
      function1(*fti);
   }

The bottom line is that your problems stem not from
boost::shared_ptr usage but from fundamental misunderstandings of
how C++ works. The Boost Users List or comp.lang.c++.moderated
are better places to handle such questions. It also appears that
you need to find a good tutorial on C++ to help you understand
things better.

-- 
Rob Stewart                           stewart_at_[hidden]
Software Engineer                     http://www.sig.com
Susquehanna International Group, LLP  using std::disclaimer;

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk