473,700 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

CD
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
Jul 22 '05 #1
2 1940
"CD" <da*****@yahoo. com> wrote in message
news:c1******** *************** ***@posting.goo gle.com...
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
Jul 22 '05 #2
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_inser ter(bList));

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

Victor
Jul 22 '05 #3

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

Similar topics

3
7095
by: alg | last post by:
dynamic_cast<> comes in play when to perform conversion from a pointer to a base class to a pointer to a derived class. I don't understand: 1. why this is so necessary since we can either use an explicit cast or use "static_cast<>" (Can we?)? 2. the above said conversion is dangerous, using explicit conversion. Does this mean that using dynamic_cast<> will be safer. How this could be? 3. Is static_cast<> at compiling time and...
11
5143
by: Scott Brady Drummonds | last post by:
Hi, everyone, I've checked a couple of on-line resources and am unable to determine how reinterpret_cast<> is different from static_cast<>. They both seem to perform a compile-time casting of one type to another. However, I'm certain that there is something else that is happening. Can someone explain the difference or recommend an online site that can explain it to me?
2
2102
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
2333
by: Andy Lomax | last post by:
I'm using a library where the supplier has provided base class 'foo', and a reference counting class which wraps 'foo': ------------------- library code ----------------------- template<class T> refcount { ... // reference counting wrapper; like shared_ptr }; class foo {
4
52980
by: matty.hall | last post by:
I have two classes: a base class (BaseClass) and a class deriving from it (DerivedClass). I have a List<DerivedClass> that for various reasons needs to be of that type, and not a List<BaseClass>. However, I need to cast that list to a List<BaseClass> and it is not working. The code is below. I get the following exception: "Unable to cast object of type 'System.Collections.Generic.List`1' to type 'System.Collections.Generic.List`1'." ...
16
2400
by: call_me_anything | last post by:
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 :
5
3839
by: David Longnecker | last post by:
I'm working to create a base framework for our organization for web and client-side applications. The framework interfaces with several of our systems and provides the business and data layer connectivity for basic operations and such. I've ran into a snag that I just can't think myself out of. Here's an example: I have an object for a Student called StudentRecord. It has properties such as name, grade, identification number, etc.
3
1564
by: int19h | last post by:
While developing my own serialization library, I've found a need to detect, for a given pair of classes B and D, such that D directly derives from B, whether B is a virtual or non-virtual base of D. I've tried a few approaches to developing a template that would be able to resolve this, trying to use the inability to static_cast virtual bases to derived classes, and conversion ambiguity in overrides with covariant return types, but it...
13
2115
by: baltasarq | last post by:
Hi, there ! When I use dynamic_cast for a single object, I do something like: void foo(Base *base) { if ( dynamic_cast<Derived *>( base ) != NULL ) { ((Derived *) base)->do_something() } }
0
8722
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9069
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8968
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
7805
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4401
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4654
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3086
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
2
2389
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.