Boost logo

Boost Users :

From: Dean Brettle (dean_at_[hidden])
Date: 2003-06-20 16:14:15


Hi Drew,

You should probably add:

void Fn(DerivedPtr& pObj)
{
  pObj = DerivedPtr(new Derived());
  pObj->m_value = 10;
}

since having a DerivedPtr (or a Derived* for that matter) point to an instance of Base is generally a bad idea. Consider what would happen if you called pDerived->method_only_in_derived().

--Dean
  ----- Original Message -----
  From: Drew
  To: Boost-Users_at_[hidden]
  Sent: Thursday, June 19, 2003 8:38 AM
  Subject: [Boost-Users] Passing shared_ptr to function that creates obj that shared_ptr points to

  [Moderator's note: The only apparent connection this message has to Boost is in the subject line. I am approving it on the assumption that the missing declarations for ParentPtr and DerivedPtr are typedefs using boost::shared_ptr.]

  Hello everyone...

  I am attempting to do the following:

  class Parent
  {
  public:
    int m_value;
  };

  class Derived : public Parent
  {
  };

  void Fn(ParentPtr& pObj)
  {
    pObj = ParentPtr(new Parent());
    pObj->m_value = 10;
  }

  int main()
  {
    ParentPtr pParent;
    DerivedPtr pDerived;

    Fn(pDerived);

    return 0;
  }

  If I pass the parent pointer, it compiles and works fine. If I pass
  the derived pointer I get:

  error C2664: 'Fn' : cannot convert parameter 1 from 'DerivedPtr'
  to 'ParentPtr &'

  Does anyone know how to solve my problem?

  TIA,
  Drew

        Yahoo! Groups Sponsor

             
       
       

  Info: <http://www.boost.org>
  Wiki: <http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl>
  Unsubscribe: <mailto:boost-users-unsubscribe_at_[hidden]>

  Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.

[Non-text portions of this message have been removed]


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net