shared_ptr and containers

Discussion in 'C++' started by Gonsolo, Sep 17, 2007.

  1. Gonsolo

    Gonsolo Guest

    Hi!

    I try to do the following:

    class A {
    };

    class B : public A {

    };

    int main()
    {
    vector< shared_ptr<A> > v;
    shared_ptr<B> d( new B );
    v.push_back( d );
    shared_ptr<B> d2 = dynamic_pointer_cast( v[0] );
    }

    and the compiler (g++) bails out with an:

    error: no matching function for call to
    'dynamic_pointer_cast(boost::shared_ptr<A>&)'


    Is dynamic_pointer_cast supposed to work with containers like vector?

    Thanks.
     
    Gonsolo, Sep 17, 2007
    #1
    1. Advertising

  2. Gonsolo

    Gonsolo Guest

    Found out myself;

    (...)
    dynamic_pointer_cast<B>
    (...)

    instead of

    (...)
    dynamic_pointer_cast
    (...)

    did it.
    Also, I had to add a virtual destructor in "A" to make it polymorphic.
     
    Gonsolo, Sep 17, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Derek
    Replies:
    2
    Views:
    627
    Derek
    Dec 8, 2003
  2. Ryan Mitchley
    Replies:
    6
    Views:
    11,167
    Maxim Yegorushkin
    Jun 26, 2004
  3. Colin Caughie
    Replies:
    1
    Views:
    718
    Shooting
    Aug 29, 2006
  4. Replies:
    7
    Views:
    554
    Pete Becker
    Jan 25, 2008
  5. Sebastian Mach
    Replies:
    5
    Views:
    313
Loading...

Share This Page