473,548 Members | 2,721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 ?

Jan 23 '07 #1
16 2372
call_me_anythin g 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.
Jan 23 '07 #2
"call_me_anythi ng" <sg***@yahoo.co mwrote in message
news:11******** *************@q 2g2000cwa.googl egroups.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<Xvx;
vector<Yvy;

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.ass ign(vec_of_deri ved.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>*.
Jan 23 '07 #3

Andrew Koenig wrote:
"call_me_anythi ng" <sg***@yahoo.co mwrote in message
news:11******** *************@q 2g2000cwa.googl egroups.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<Xvx;
vector<Yvy;

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...ma ybe something like so:

template < typename other_val_type >
enable_if
<
mpl::and_
<
is_pointer<valu e_type>
, is_pointer<othe r_val_type>
, is_base_of<valu e_type, other_val_type>
>
, vector &
>
operator = ( vector<other_va l_typeconst & right)
{
....??
}

Jan 23 '07 #4
On Jan 23, 8:31 pm, "call_me_anythi ng" <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_cas t vec_of_base. Though, having been never tried out by
me, it seems reasonable.

Jan 24 '07 #5
call_me_anythin g 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.ass ign(vec_of_deri ved.begin(), vec_of_derived. end());

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

Joe Gottman
Jan 24 '07 #6
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=*re interpret_cast< vector<Base*>*> (&vec_of_derive d)

This does work, at least under MSVS.

Jan 24 '07 #7
"Pavel Shved" <Pa*********@gm ail.comwrote in message
news:11******** **************@ m58g2000cwm.goo glegroups.com.. .
I became curious enough to check it for myself.
vec_of_base=*re interpret_cast< vector<Base*>*> (&vec_of_derive d)
This does work, at least under MSVS.
If it works, it does so only by coincidence.
Jan 24 '07 #8


On Jan 24, 8:54 am, "Andrew Koenig" <a...@acm.orgwr ote:
vec_of_base=*re interpret_cast< vector<Base*>*> (&vec_of_derive d)
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.

Jan 24 '07 #9


On Jan 23, 1:33 pm, "Noah Roberts" <roberts.n...@g mail.comwrote:
whenever it is possible to convert Y to X.You could use a combination of enable_if, is_pointer, and
is_base_of...ma ybe something like so:

template < typename other_val_type >
enable_if
<
mpl::and_
<
is_pointer<valu e_type>
, is_pointer<othe r_val_type>
, is_base_of<valu e_type, other_val_type>
>
, vector &
// HERE
operator = ( vector<other_va l_typeconst & 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.

Jan 24 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
9196
by: Weston C | last post by:
I'm trying to get the href attribute from the base tag in the document head. The following does not seem to work... any ideas what I'm doing wrong? if(!(document.getElementsByTagName) || !(basehrefs = document.getElementsByTagName('base')) || !(basehrefs.length) || !(basehrefs.length > 0) || !(basehrefs.getAttribute) || !(basehref =...
2
1932
by: CD | last post by:
Is this possible: class base; class derived; //:public base vector <base*> bList; vector<derived*> dList; //add some derived class pointer entries to dList;
7
4721
by: Frank Bormann | last post by:
Hi guys, I'm a C++ beginner. I was wondering if there is a generalized vector base class, through which I could pass a pointer to any kind of vector to a function, regardless of what type of objects the passed vector is holding. Regards, Frank
2
2093
by: Siemel Naran | last post by:
This code fails compile std::auto_ptr<Base> f() { std::auto_ptr<Derived> out(new Derived()); return out; } There is ambiguity between a templated constructor and templated operator conversion, according to my compiler. Seems there are too many constructors and operator conversions. But this code works:
3
1613
by: Gilbert Saint-Flour | last post by:
Hello: I have this one line of PHP code which I'd like to convert to Javascript. The PHP code conditionally issues a BASE statement when the html page is called with a frame name, e.g. href=index2.php?frame2 <html> <head> <? $tag = $_SERVER; if(empty($tag)) {; } else { echo "<base target=$tag>"; } ?> </head>
1
2713
by: Chris Sharman | last post by:
I'm seeing quite a few 404 errors, all from an agent/browser which calls itself "Mozilla/4.0 (compatible; BorderManager 3.0)". They appear to be from a built page like http://services.ccagroup.co.uk/upbin/oformw?&whsmithwedding (no referrer supplied, so I can't be certain), which contains <base...
3
4577
by: SJ | last post by:
Hi, I have a problem with the HTML <base> element, URL re-writing and Postback. We are using URL re-writing on the server, and I'd like to use the base element to make the URLs in the ASPX pages more maintainable. Otherwise, we have to manually output the application root eg. src="<%= AppRoot %>". This is cumbersome and hard to maintain. ...
3
12838
by: yan | last post by:
Hello everybody, I am new so this is the occasion to say hello to everybody. I have a problem with absolute/relative paths. I have to create a static documentation in html for a project and I have to create an index page in every sub-directory. The structure is like the following: / /sources/ /docs/ /docs/doc1 /share
0
7518
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7444
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7467
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6039
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5085
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3478
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1932
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1054
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
755
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.