Noobie Question about casting SmartPtr

Help
2007-05-17
2013-04-08
  • Nobody/Anonymous

    Hi All,
    I'm new to Loki, so "be gentle". Anyway, I'm trying to convert my code over to using SmartPtr. I have some
    places where I try to dynamic_cast a pointer but I don't see how to do this if my class is now using a SmartPtr. Example:
    Say that I have two classes, base class A and derived class B:
    class A;
    class B:public A;

    How would I convert the following code to use Loki::SmartPtr instead of regular pointers?
    ...
       bool SomeFunction(A* ptrA)
       {
          B* ptrB = dynamic_cast<B*>(ptrA);
          if(ptrB)
          {
             return true;
          }
          return false;
       }

    Thanks for any help.

     
    • Peter Kuemmel

      Peter Kuemmel - 2007-05-17

      You must cast the raw pointer:
      dynamic_cast<B*>(ptrA->GetPointer());

      Peter

       
      • Alber Kami

        Alber Kami - 2008-07-24

        Hello,

        If you dynamic_cast the raw pointer you can't use the result in SmartPtr any more!
        There should be a way to down cast a SmartPtr<Base> to SmartPtr<Derived> conserving the SmartPtr logic.

        Is eny one know how to do it, or why isn't it implemented ?
        rich_sposato said the ability to get raw pointer was removed with StrongPtr,
        What one should do if he wants to down cast StrongPtr ?

        Shaul

         
    • Nobody/Anonymous

      sorry for this delayed response.
      I tried changing my calls to:

      dynamic_cast<B*>(ptrA->GetPointer());

      but I received the following compilation error:

      1>c:\bivdev\ve-suite\ve_open\xml\cad\cadnodetraverser.cxx(113) : error C2039: 'GetPointer' : is not a member of 'VE_XML::VE_CAD::CADNode'
      1>        c:\bivdev\ve-suite\ve_open\xml\cad\cadnode.h(67) : see declaration of 'VE_XML::VE_CAD::CADNode'

      which tells me that it tried to resolve the operator-> on the actual class rather than using SmartPtr functionality. So I then tried using
      .GetPointer() but this requires some input parameter. Is that the method I should be using? What is the syntax of the input to use?

      Thanks

       
    • Nobody/Anonymous

      Actually, I see that that function is static so I can just use SmartPtr::GetPointer(prtA). Tried this
      but after ptrB looses scope, the memory in ptrA is no good anymore...Any ideas why that would be?

       
      • Richard Sposato

        Richard Sposato - 2007-05-24

        > Tried this but after ptrB looses scope, the memory in ptrA is no good anymore...
        > Any ideas why that would be?

        From original post:
        >  B* ptrB = dynamic_cast<B*>(ptrA);

        I can think of two reasons why the memory at ptrA becomes useless.

        1. Did you assign ptrB to some other SmartPtr?  Did some function called later assign ptrB to a SmartPtr?  If so, you have a SmartPtr pointing to the same chunk of memory as ptrA, but ptrA and the other SmartPtr don't know about each other. When the other SmartPtr dies, it destructs the object and releases the memory.

        2. Did you delete the object at ptrB unknowingly - perhaps by passing ptrB into some function that assumes ownership of the object and destroys it.  If so, the memory gets released as the object dies.

        These reasons, among others, are why people say to never let a smart pointer provide direct access to the plain pointer.  The newer version of StrongPtr class does not provide access to the GetPointer function for just those reasons.

        Hope that helps.

        Cheers,

        Rich

         
  • Richard Sposato

    Richard Sposato - 2009-11-10

    Thanks!  Your patch has been tested and implemented for both SmartPtr and StrongPtr.

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks