Boost logo

Boost :

From: H (h.v.niekerk_at_[hidden])
Date: 2005-06-21 10:44:19


Martin Bonner wrote:
> ----Original Message----
> From: H. van Niekerk [mailto:h.v.niekerk_at_[hidden]]
> Sent: 20 June 2005 19:22
> To: boost_at_[hidden]
> Subject: [boost] shared_ptr question
>
>
>>Hi,
>>
>>I'm rather new to shared_ptr and I'm not very experienced in
>>programmer
>>with C++ but have to do it anyway.
>>My problem is this: I have 3 .cpp files, e.g. Master.cpp, Client.cpp
>>and Satellite.cpp.
>>
>>Originally, Master.cpp is the only .cpp file, and uses auto_ptr to
>>enable writing and reading from an interface. Like this:
>>
>>{
>> ....
>> auto_ptr<FtInterface> fti (ftGetIfFromFactory(device,
>> connection));
>> fti->getIfInfo(ii);
>> IfOutputs out; // to set interface outputs (ctor clears
>> // everything to 0)
>> IfInputs inp; // to be read from interface
>> fti->writeAndReadAllData(out, inp);
>> ...
>>}
>>
>>This works well with every action taking place in this file. Now, as
>>said I have those 3 .cpp files. Master.cpp sends an argument to
>>Client.cpp and Client.cpp sends it to Satellite.cpp. Satellite.cpp
>>reads
>>from the interface, and sends it back via Client.cpp to Master.cpp. I
>>can't use auto_ptr with this and from the Boost-documentation I can't
>>figure out exactly how to use shared_ptr for this. Can somebody please
>>help me out?
>>
>>I hope I explained my problem clear enough.
>
>
> I'm afraid you didn't. Could you write some sample (pseudo-) code just
> using raw C pointers and not worrying about memory leaks? Then we can
> probably work out how to rewrite using smart pointers.
>
> Incidentally, I don't think the presence of three .cpp files is directly
> relevent. I suspect that the relevent factor is that you have three
> *functions*, and your problem is passing the data between them.
>
>
>
Thank you for your response. I hope the code below explains more:

Master.cpp
{
        Client c;
        c.readFromSatellite(input);
        cout << "input = " << input << endl;
}

Client.cpp
{
        Satellite s;

        readFromSatellite(input)
        {
                s.readFromInterface(input);
                return input;
        }
}

Satellite.cpp
{
        readFromInterface(input)
        {
                // Here the actual reading is done
                fti->writeAndReadAllData(output, input);
                return input;
        }
}


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