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
Drew wrote:
void Fn(ParentPtr& pObj) { pObj = ParentPtr(new Parent()); pObj->m_value = 10; }
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 &'
You can't assign to a shared_ptr (assuming ParentPtr is a shared_ptr as the moderator has suggested). You have to call reset on it e.g. pObj.reset(new Parent()); HTH Russell
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@yahoogroups.com 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@yahoogroups.com Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service. [Non-text portions of this message have been removed]
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@yahoogroups.com 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
To be a little more detailed, here is what I am trying to accomplish:
typedef boost::shared_ptr<CBase> BasePtr;
typedef boost::shared_ptr<CDerived> DerivedPtr;
AddObject(pBase); // works
AddObject(pDerived); // does not work
void AddObject(BasePtr& pObj)
{
std::vector<BasePtr>::iterator pos = find(myList.begin(),
myList.end(), 5);
if (pos != myList.end()) {
pObj= *pos;
}
else {
myList.push_back(pObj);
}
}
error C2664: 'Fn' : cannot convert parameter 1 from 'DerivedPtr'
to 'ParentPtr &'
A reference that is not to 'const' cannot be bound to a non-lvalue
However,
AddObject(base); // works
AddObject(derived); // works
void AddObject(CBase& base)
{
}
This is a generic function that adds an element if it does not exist.
Why when I use a shared_ptr does this not work?
I dont want a specific lookup for all of my objects.
Thanks guys...
--- In Boost-Users@yahoogroups.com, "Dean Brettle"
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 &' A reference that is not to 'const' cannot be bound to a non-lvalue 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@yahoogroups.com
Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
[Non-text portions of this message have been removed]
This is normal behaviour for a pointer too, so shared_ptr is correct to disallow this : void AddObject(CBase&* pObj); ... CDerived* pDerived = 0;. AddObject(p); // also won't compile If you think about it this makes sense. Imagine, if that behaviour were allowed this would cause some problems: class CDerived2 : public CBase {}; void AddObject(CBase&* pObj) { pObj = new CDerived2; } ... CDerived* pDerived = 0; AddObject(pDerived); // eeek! pDerived now points to a CDerived2 object! Normally in this situation I would return a shared_ptr<> rather than take one by reference. (I prefer that style anyway, but lets not get into that :-) ). You will then have to do a cast to the derived object ptr: BasePtr AddObject(); ... BasePtr p = AddObject(); if (DerivedPtr pDerived = shared_dynamic_cast<CDerived>(p)) { // ... } HTH Sam Drew wrote:
To be a little more detailed, here is what I am trying to accomplish:
typedef boost::shared_ptr<CBase> BasePtr; typedef boost::shared_ptr<CDerived> DerivedPtr;
AddObject(pBase); // works AddObject(pDerived); // does not work
void AddObject(BasePtr& pObj) { std::vector<BasePtr>::iterator pos = find(myList.begin(), myList.end(), 5);
if (pos != myList.end()) { pObj= *pos; } else { myList.push_back(pObj); } }
error C2664: 'Fn' : cannot convert parameter 1 from 'DerivedPtr' to 'ParentPtr &' A reference that is not to 'const' cannot be bound to a non-lvalue
However,
AddObject(base); // works AddObject(derived); // works
void AddObject(CBase& base) { }
This is a generic function that adds an element if it does not exist.
Why when I use a shared_ptr does this not work?
I dont want a specific lookup for all of my objects.
Thanks guys...
participants (4)
-
Dean Brettle
-
Drew
-
Russell Hind
-
Sam Partington