Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > vector <Base *> = vector <Derived *> ??

Reply
Thread Tools

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

 
 
call_me_anything
Guest
Posts: n/a
 
      01-23-2007
why is the following not allowed :

vector <Base *> vec_of_base;
vector <Derived *> vec_of_derived;
vec_of_base = vec_of_derived;



Note : The following is allowed :

Base *base_ptr;
Derived *derived_ptr;
base_ptr = derived_ptr;

How do things change when we use a vector ?

 
Reply With Quote
 
 
 
 
Mark P
Guest
Posts: n/a
 
      01-23-2007
call_me_anything wrote:
> why is the following not allowed :
>
> vector <Base *> vec_of_base;
> vector <Derived *> vec_of_derived;
> vec_of_base = vec_of_derived;
>
>


Because vector<Derived*> is not derived from vector<Base*>. In fact,
considering the possibility of template specialization, vector<Derived*>
may be wildly different than vector<Base*>.

>
> Note : The following is allowed :
>
> Base *base_ptr;
> Derived *derived_ptr;
> base_ptr = derived_ptr;
>
> How do things change when we use a vector ?
>


It's not unique to a vector. A relation between the template parameters
does not imply any relationship between the template classes.
 
Reply With Quote
 
 
 
 
Andrew Koenig
Guest
Posts: n/a
 
      01-23-2007
"call_me_anything" <> wrote in message
news: ps.com...

> why is the following not allowed :
>
> vector <Base *> vec_of_base;
> vector <Derived *> vec_of_derived;
> vec_of_base = vec_of_derived;


It's hard to understand how one would implement such a feature without also
allowing

vector<X> vx;
vector<Y> vy;

vx = vy;

whenever it is possible to convert Y to X. Such assignments could be
allowed in principle, but they can also be hazardous and inefficient. If
you want to achieve that effect, you can do so fairly easily:

vec_of_base.assign(vec_of_derived.begin(), vec_of_derived.end());

Please note that this question is completely different from the commonly
asked question about why it is not possible to convert a vector<Derived>* to
a vector<Base>*.


 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      01-23-2007

Andrew Koenig wrote:
> "call_me_anything" <> wrote in message
> news: ps.com...
>
> > why is the following not allowed :
> >
> > vector <Base *> vec_of_base;
> > vector <Derived *> vec_of_derived;
> > vec_of_base = vec_of_derived;

>
> It's hard to understand how one would implement such a feature without also
> allowing
>
> vector<X> vx;
> vector<Y> vy;
>
> vx = vy;
>
> whenever it is possible to convert Y to X.


You could use a combination of enable_if, is_pointer, and
is_base_of...maybe something like so:

template < typename other_val_type >
enable_if
<
mpl::and_
<
is_pointer<value_type>
, is_pointer<other_val_type>
, is_base_of<value_type, other_val_type>
>

, vector &
>

operator = ( vector<other_val_type> const & right)
{
....??
}

 
Reply With Quote
 
Pavel Shved
Guest
Posts: n/a
 
      01-24-2007
On Jan 23, 8:31 pm, "call_me_anything" <s...@yahoo.com> wrote:
> why is the following not allowed :
>
> vector <Base *> vec_of_base;
> vector <Derived *> vec_of_derived;
> vec_of_base = vec_of_derived;
> ...
> How do things change when we use a vector ?


Well, and if we call these types not Base and Derived, having the
assignment base=derived being correct, will these vectors be equal,
without derivation relationships? We will have to do element-by-element
assignment anyway.

Although in this case there should work a trick, based on the equality
of Base* and Derived* entities -- they are pointers with same size. I'm
not quite sure whether it's correct, but i was once told to try
reinterpret_cast vec_of_base. Though, having been never tried out by
me, it seems reasonable.

 
Reply With Quote
 
Joe Gottman
Guest
Posts: n/a
 
      01-24-2007
call_me_anything wrote:
> why is the following not allowed :
>
> vector <Base *> vec_of_base;
> vector <Derived *> vec_of_derived;
> vec_of_base = vec_of_derived;
>
>


Note that the following is legal:

vector<Base *> vec_of_base;
vector<Derived *> vec_of_derived;
vec_of_base.assign(vec_of_derived.begin(), vec_of_derived.end());

Admittedly it's much wordier than your version, but it compiles and does
what you want.

Joe Gottman
 
Reply With Quote
 
Pavel Shved
Guest
Posts: n/a
 
      01-24-2007
> why is the following not allowed :
>
> vector <Base *> vec_of_base;
> vector <Derived *> vec_of_derived;
> vec_of_base = vec_of_derived;


I became curious enough to check it for myself.

vec_of_base=*reinterpret_cast<vector<Base*>*>(&vec _of_derived)

This does work, at least under MSVS.

 
Reply With Quote
 
Andrew Koenig
Guest
Posts: n/a
 
      01-24-2007
"Pavel Shved" <> wrote in message
news: oups.com...

> I became curious enough to check it for myself.


> vec_of_base=*reinterpret_cast<vector<Base*>*>(&vec _of_derived)


> This does work, at least under MSVS.


If it works, it does so only by coincidence.


 
Reply With Quote
 
Pavel Shved
Guest
Posts: n/a
 
      01-24-2007


On Jan 24, 8:54 am, "Andrew Koenig" <a...@acm.org> wrote:
> > vec_of_base=*reinterpret_cast<vector<Base*>*>(&vec _of_derived)


> If it works, it does so only by coincidence.


Yes, coincidence is the exact reason why it does work. vector
<Base*> and vector <Derived*> are be byte-to-byte equal.

 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      01-24-2007


On Jan 23, 1:33 pm, "Noah Roberts" <roberts.n...@gmail.com> wrote:

> > whenever it is possible to convert Y to X.You could use a combination of enable_if, is_pointer, and

> is_base_of...maybe something like so:
>
> template < typename other_val_type >
> enable_if
> <
> mpl::and_
> <
> is_pointer<value_type>
> , is_pointer<other_val_type>
> , is_base_of<value_type, other_val_type>
> >

> , vector &
> > // HERE

> operator = ( vector<other_val_type> const & right)
> {
> ...??
>
> }


I don't normally respond to people that email me instead of posting to
the group but I decided in this case I would. Someone asked me two
questions:

1) Are these standard components or did I just make them up?

It is a combination of standard stuff and things that are found in
certain boost libraries. Some of it, including the is_pointer and
is_base_of traits, will be in the next standard. These are basic
metaprogramming techniques but if you've never seen metaprogramming it
might be rather difficult to understand.

2) Where does the last > go?

Apparently google groups removed it. It should be on the line after
vector& and before "operator", I put it back.

Now, newsgroup etiquette says you post your replies to the group, not
the user. This is not only because it might be considered rude but so
that others can see the answer...now you know.

 
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
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
how the vector is created, how to pass vector to webservices method apachesoap:Vector Rushikesh Joshi Perl Misc 0 07-10-2004 01:04 PM
how do i create a vector within a vector ? learningjava Java 5 10-17-2003 10:19 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57