From: Perry Smith (pedz_at_[hidden])
Date: 2006-02-15 18:45:45


Hi,

I often have to "fight" with my C++ code and I must be missing a
basic design characteristic of C++.

Suppose there is a shared_ptr that has already been created. I now
want to create an object with new and "give" it to shared_ptr to
manage. I tend to do this:

std::tr1::shared_ptr<int> dog;

int fun(void)
{
     int *temp = new int;
     dog = temp;
     // ...
}

This does not work. I've discovered two ways to achieve (I think)
what I'm looking for:

int fun(void)
{
     std::tr1::shared_ptr<int> temp(new int);
     dog = temp;
     // ...
}

or ...

int fun(void)
{
     foo *temp = new int;
     dog.reset(temp);
     // ...
}

The first has the extra overhead of creating a second shared_ptr.
The second worries me... It just seems like "reset" is not what I
would call the method to do what I want to do (but maybe it is).

Anyone care to help me out here?

Thanks,
Perry