|
Boost Users : |
From: Drew (drewtenn_at_[hidden])
Date: 2003-06-20 22:42:56
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_at_[hidden], "Dean Brettle" <dean_at_b...> wrote:
> 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 &'
> 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_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