Boost logo

Boost :

From: H (h.v.niekerk_at_[hidden])
Date: 2005-06-22 00:45:01


> 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);
}

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));
}

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

Thank you for helping.

Huub


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