473,761 Members | 8,813 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

casting an vector::iterato r to a derived type to the base class

Hello, say I have two classes:

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

I would like to do something similar to this:

std::vector<Bas e*>::iterator b;
std::vector<Der ived*>::iterato r d;

d = vector_of_deriv ed.begin();
b = *((std::vector< Base*>::iterato r*)(&d));

Am I correct that this a really stupid thing to do? Is there a better
way of going about this? Thanks for any help!

-Mike

Mar 28 '07 #1
4 5326
pr**********@gm ail.com wrote:
Hello, say I have two classes:

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

I would like to do something similar to this:

std::vector<Bas e*>::iterator b;
std::vector<Der ived*>::iterato r d;

d = vector_of_deriv ed.begin();
b = *((std::vector< Base*>::iterato r*)(&d));

Am I correct that this a really stupid thing to do? Is there a better
way of going about this? Thanks for any help!
don't even bother with the d variable.

Base* b_ptr = *b;
Derived* d_ptr = dynamic_cast<De rived*>(b_ptr);

But is there a reason you need to know if it's a Base or Derived in this
situation? If so, then you probably should have two containers,
otherwise, just rely on polymorphic behavior in Base.
Mar 28 '07 #2
pr**********@gm ail.com wrote:
Hello, say I have two classes:

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

I would like to do something similar to this:

std::vector<Bas e*>::iterator b;
std::vector<Der ived*>::iterato r d;

d = vector_of_deriv ed.begin();
b = *((std::vector< Base*>::iterato r*)(&d));

Am I correct that this a really stupid thing to do? Is there a better
way of going about this? Thanks for any help!
Yes, it is really stupid thing to do! Iterators are unrelated types, so
casting between different iterators is not a good idea.

Why not just use a vector of Base*?

--
Ian Collins.
Mar 28 '07 #3
Here's my issue. I have a vector<Derived* >, and an interface that
takes a vector<Base*>:: iterator. Is there something as a proxy
iterator class that will do the conversion automatically?

On Mar 28, 6:37 pm, Ian Collins <ian-n...@hotmail.co mwrote:
propokerg...@gm ail.com wrote:
Hello, say I have two classes:
class Base{...};
class Derived : public Base{...}
I would like to do something similar to this:
std::vector<Bas e*>::iterator b;
std::vector<Der ived*>::iterato r d;
d = vector_of_deriv ed.begin();
b = *((std::vector< Base*>::iterato r*)(&d));
Am I correct that this a really stupid thing to do? Is there a better
way of going about this? Thanks for any help!

Yes, it is really stupid thing to do! Iterators are unrelated types, so
casting between different iterators is not a good idea.

Why not just use a vector of Base*?

--
Ian Collins.

Mar 30 '07 #4
pr**********@gm ail.com wrote:

Please don't top-post.
>
On Mar 28, 6:37 pm, Ian Collins <ian-n...@hotmail.co mwrote:
>>propokerg...@ gmail.com wrote:
>>>Hello, say I have two classes:
>>>class Base{...};
class Derived : public Base{...}
>>>I would like to do something similar to this:
>>>std::vector< Base*>::iterato r b;
std::vector< Derived*>::iter ator d;
>>>d = vector_of_deriv ed.begin();
b = *((std::vector< Base*>::iterato r*)(&d));
>>>Am I correct that this a really stupid thing to do? Is there a better
way of going about this? Thanks for any help!

Yes, it is really stupid thing to do! Iterators are unrelated types, so
casting between different iterators is not a good idea.

Why not just use a vector of Base*?
Here's my issue. I have a vector<Derived* >, and an interface that
takes a vector<Base*>:: iterator. Is there something as a proxy
iterator class that will do the conversion automatically?
Not easily, no. The iterators are different types.

Can't you change the vector<Derived* to a vector<Base*>?

--
Ian Collins.
Mar 30 '07 #5

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

Similar topics

1
5610
by: Steven Lien | last post by:
I wrote a simple vector class that can store Cards, and my question is if i had Card class, should i "new" it to store into vector , or simply use copy structure?. if i need the vector class to be dynamic?... IE: vector<Card>* p = new vector<Card>(52); or
2
2209
by: Eric | last post by:
I am converting my VC++ MFC to Manage C++ and I am having trouble using the vector class. Here is my __gc class ....other headers for System classes .... #include <algorithm> #include <vector> using namespace std;
3
2679
by: Lars Grobe | last post by:
Hi, first hello, I am new to the list, and I guess my question will show that clearly. I want to use some vector operations (at the moment altivec) in existing code. It is a raytracing-based application, and for now, I would like to replace the operations on my data type vect3 (which is a float array) by operations on a vector float. The vect3 is simply a typedef to an array of floats so far.
6
1345
by: Martin S. | last post by:
the class: public __gc class Vector { private: Double y, x, z; public: Vector() { y(0),
3
4873
by: cagenix | last post by:
I was running through a data structures book and I was curious if anyone could inform me of how to inherit the vector class to do a simple search and erase function. The example states: vector_plus is a subclass of the vector class. vector_plus has no new fields. Define: void erase_item(const T& item); Here is an example of my header file:
2
2368
by: vermarajeev | last post by:
I'm unable to use char* or string in Vector class. I know what the problem is but unable to find a proper answer. One solution is like I can call explicit intialization for char* or string and perform operations for only those but unable to get how to do that. Can someone help please. I want Vector to work with any datatype. template<class T>
12
2188
by: mast2as | last post by:
Hi everyone I am working on some code that uses colors. Until recently this code used colors represented a tree floats (RGB format) but recently changed so colors are now defined as spectrum. The size of the vector went from 3 (RGB) to 151 (400 nm to 700 with a sample every 2nm). The variables are using a simple Vector class defined as follow: template<typename T, int Depth> class Vector
1
1380
by: Daniel.Wyatt | last post by:
I need some help with this please. Here's the important part: #include <vector> #include <string> ................ template <class T> bool ResourceManager<T>::isLoaded(const std::string Filename) { std::vector<T>::iterator it; //error: expected `;' before "it" return false;
5
2783
by: Jazi | last post by:
I am newbie to c++ and I would like to build a vector class that returns a vector of objects. I have done some thing like this: student class that contains some of student's properties such as name, address, phone etc. I also have a registered students class which I like to have it as a vector of student objects. The way I have done it so far is like this. RegisteredStduents class{ RegisteredStudents(no_of_students); ...
0
10136
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9988
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
9923
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
8813
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
7358
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
6640
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.