Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Automatic Conversion of STL Containers: e.g. from vector<derived*> to vector<base*>

Reply
Thread Tools

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

 
 
CD
Guest
Posts: n/a
 
      10-05-2004
Is this possible:

class base;
class derived; //ublic 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
 
Reply With Quote
 
 
 
 
Ivan Vecerina
Guest
Posts: n/a
 
      10-05-2004
"CD" <> wrote in message
news: om...
> Is this possible:
>
> class base;
> class derived; //ublic 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


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-05-2004
CD wrote:
> Is this possible:
>
> class base;
> class derived; //ublic 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
There is need for Text to XML semi/automatic conversion? Francesc XML 3 12-29-2005 02:47 PM
XML Automatic conversion to Unicode and Problems with the order ofattributes Raphael A. Bauer Java 0 02-17-2005 12:32 PM
Java - Automatic conversion to Unicode and Problems with the orderof attributes Raphael A. Bauer XML 0 02-17-2005 08:48 AM
how to prevent automatic conversion of .csv file to .xls =?Utf-8?B?RGl2eWE=?= ASP .Net 6 08-11-2004 11:41 AM
Automatic build process + automatic NuNit (2 in 1 solution) ASP .Net 1 06-29-2004 04:15 PM



Advertisments