Automatic Conversion of STL Containers: e.g. from vector<derived*> to vector<base*>

Discussion in 'C++' started by CD, Oct 5, 2004.

  1. CD

    CD Guest

    Is this possible:

    class base;
    class derived; //:public base

    vector <base*> bList;
    vector<derived*> dList;

    //add some derived class pointer entries to dList;

    bList = dList;
    //OR bList = (vector <base*>) dList;

    //use the entries as base class pointers


    - CD
     
    CD, Oct 5, 2004
    #1
    1. Advertising

  2. "CD" <> wrote in message
    news:...
    > Is this possible:
    >
    > class base;
    > class derived; //:public base
    >
    > vector <base*> bList;
    > vector<derived*> dList;
    >
    > //add some derived class pointer entries to dList;
    >
    > bList = dList;

    No: assignment will only work if both vectors have the same type.
    > //OR bList = (vector <base*>) dList;

    No: something similar *might* work in practice, but would be UB
    (undefined behavior).

    The modified code below will work, however:

    #include <vector>
    using namespace std;

    class Base {};
    class Derived : public Base {};

    void f( vector<Derived*> const d, vector<Base*>& b )
    {
    b.assign( d.begin(), d.end() ); // ok, copies & converts the pointers
    // b now has a (converted) copy of the contents of b
    }


    hth,
    Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Brainbench MVP for C++ <> http://www.brainbench.com
     
    Ivan Vecerina, Oct 5, 2004
    #2
    1. Advertising

  3. Re: Automatic Conversion of STL Containers: e.g. from vector<derived*>to vector<base*>

    CD wrote:
    > Is this possible:
    >
    > class base;
    > class derived; //:public base
    >
    > vector <base*> bList;
    > vector<derived*> dList;
    >
    > //add some derived class pointer entries to dList;
    >
    > bList = dList;
    > //OR bList = (vector <base*>) dList;
    >
    > //use the entries as base class pointers


    Yes, it is possible. Since 'derived*' is convertible to 'base*',
    a simple 'std::copy' should suffice:

    std::copy(dList.begin(), dList.end(), std::back_inserter(bList));

    Since vector<base*> and vector<derived*> are not the same type, and even
    not related, an assignment cannot work.

    Victor
     
    Victor Bazarov, Oct 5, 2004
    #3
    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. Replies:
    8
    Views:
    1,930
    Csaba
    Feb 18, 2006
  2. call_me_anything

    vector <Base *> = vector <Derived *> ??

    call_me_anything, Jan 23, 2007, in forum: C++
    Replies:
    16
    Views:
    613
    Noah Roberts
    Jan 24, 2007
  3. Replies:
    1
    Views:
    397
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    389
    Victor Bazarov
    May 23, 2007
  5. =?ISO-8859-2?Q?Gordan_Kre=B9i=E6?=

    STL containers and abstract/derived classes

    =?ISO-8859-2?Q?Gordan_Kre=B9i=E6?=, Aug 16, 2007, in forum: C++
    Replies:
    1
    Views:
    509
    Frank Birbacher
    Aug 16, 2007
Loading...

Share This Page