473,396 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

casting an vector::iterator 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<Base*>::iterator b;
std::vector<Derived*>::iterator d;

d = vector_of_derived.begin();
b = *((std::vector<Base*>::iterator*)(&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 5259
pr**********@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*>::iterator b;
std::vector<Derived*>::iterator d;

d = vector_of_derived.begin();
b = *((std::vector<Base*>::iterator*)(&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<Derived*>(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**********@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*>::iterator b;
std::vector<Derived*>::iterator d;

d = vector_of_derived.begin();
b = *((std::vector<Base*>::iterator*)(&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.comwrote:
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*>::iterator b;
std::vector<Derived*>::iterator d;
d = vector_of_derived.begin();
b = *((std::vector<Base*>::iterator*)(&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**********@gmail.com wrote:

Please don't top-post.
>
On Mar 28, 6:37 pm, Ian Collins <ian-n...@hotmail.comwrote:
>>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*>::iterator b;
std::vector<Derived*>::iterator d;
>>>d = vector_of_derived.begin();
b = *((std::vector<Base*>::iterator*)(&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
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...
2
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...
3
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...
6
by: Martin S. | last post by:
the class: public __gc class Vector { private: Double y, x, z; public: Vector() { y(0),
3
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: ...
2
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...
12
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....
1
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...
5
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...
0
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,...

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.