shared_ptr == NULL???

Started by
15 comments, last by xstreme2000 17 years, 10 months ago
I'm sure this is a very simple question but I'm not overly familiar with the STL/Boost. If I've got a function which returns a smart pointer (more specifically a share_ptr) and a want set/check it for a NULL value how do I do it... e.g. (not using shared_ptr)

Object *LoadObject(string name)
{
    Object *obj;

    if (obj = somehowLoadObject())
    {
         return obj;
    }
    else
    {
         return NULL;
    }
}

// Then in main()...
Object *o;
if ((o = LoadObject("fred.obj")) == NULL) {
    cerr << "Object not loaded." << endl;
}
my guess this far

shared_ptr<Object> LoadObject(string name)
{
    shared_ptr<Object> obj;

    if (obj = somehowLoadObject())
    {
         return obj;
    }
    else
    {
         obj.reset(); // NULL pointer equivelent???
         return obj;
    }
}

// Then in main()...
shared_ptr<Object> o;
if ( /* WHAT TEST GOES HERE? */ ) {
    cerr << "Object not loaded." << endl;
}
Advertisement

shared_ptr<Object> LoadObject(string name)
{
// this might have to be return shared_ptr<Object>(somhowLoadObject());
return somehowLoadObject();
}

// calling function

shared_object<Object> o = LoadObject("fred");
if (!o)
{ cout << "Fred Not Loaded";
}
"Is life so dear, or peace so sweet, as to be purchased at the price of chains and slavery?" - Patrick Henry
hmm, is that really the only way to do it? Reason this concerns me is because my load function is storing the pointer somewhere else too and this I assume would end up meaning I'd have to shared_ptrs to the same block of memory which I would assume would mean it would get deleted twice (am I wrong)?

shared_ptr<Object> StashedObject;shared_ptr<Object> LoadObject(string name){    Object *obj = somehowLoadObject();    StashedObject.reset(obj);    return obj;}// calling functionshared_object<Object> o = LoadObject("fred");if (!o){    cout << "Fred Not Loaded";}
It's perfectly fine to have two shared_ptrs to the same object. That's why they call them SHARED_ptr.
Quote:Original post by Sneftel
It's perfectly fine to have two shared_ptrs to the same object. That's why they call them SHARED_ptr.


Oh? wicked, I always assumed you had to have more than one pointing to the same object by assigning 1 to an other, if that's not the case then great :)
Quote:Original post by Madhed
Pay attention though!

AFAIK

"if (obj = somehowLoadObject())"

does NOT equal "if ((obj = somehowLoadObject()))"

(see the extra braces?)

the first will always return true while the latter will return the value of obj after the assignment operation.

Uhhhh....... since when?
Quote:Original post by Sneftel
Uhhhh....... since when?


Erm, I think he might have confused with thinking about these 2 conditions

if (obj = loadobject() == NULL) doSomething();

and

if ((obj = loadobject()) == NULL) doSomething();
Quote:Original post by Sneftel
Uhhhh....... since when?

I am wondering the same thing.
Um... sorry for the confusion. I deleted my post.

I just went to VS.Net to check... you're absolutely right! O_o

Maybe I gathered that "knowledge" when I still programmed with the "good old" VC++6.0.
In VC++6.0 I always had to do that and now I still do it...

sorry again
Quote:Original post by Sneftel
It's perfectly fine to have two shared_ptrs to the same object. That's why they call them SHARED_ptr.

Isn't that only when the all shared_ptrs except the first one are constructed/assigned from the first shared_ptr, rather than being constructed from the raw pointer itself? In his code, it appears as though he's resetting StashedObject to reference obj, and is then returning obj, which should cause the creation of another shared_ptr. (Except I'm expecting that part to fail, if I remember correctly, because a shared_ptr must be constructed explicitly, right?) So even with an explicit constructor on the return:
shared_ptr<Object> LoadObject(string name){    Object *obj = somehowLoadObject();    StashedObject.reset(obj);    return shared_ptr<Object>(obj);}
Wouldn't that create two independent sets of shared_ptrs that point to the same thing, each thinking they own that block of memory? And thus get the double-delete issue that xstreme2000 was worried about? I would think that this would be the proper way to do it:
shared_ptr<Object> LoadObject(string name){    Object *obj = somehowLoadObject();    StashedObject.reset(obj);    return StashedObject;}
By creating a copy of StashedObject, you still only have one set of shared_ptrs. StashedObject is the only one that is ever constructed/reset from the raw pointer.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke

This topic is closed to new replies.

Advertisement