Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > casting an vector::iterator to a derived type to the base class

Reply
Thread Tools

casting an vector::iterator to a derived type to the base class

 
 
propokergrad@gmail.com
Guest
Posts: n/a
 
      03-28-2007
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

 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      03-28-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      03-28-2007
(E-Mail Removed) 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.
 
Reply With Quote
 
propokergrad@gmail.com
Guest
Posts: n/a
 
      03-30-2007
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 <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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.



 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      03-30-2007
(E-Mail Removed) wrote:

Please don't top-post.
>
> On Mar 28, 6:37 pm, Ian Collins <(E-Mail Removed)> wrote:
>
>>(E-Mail Removed) 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.
 
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
Casting from base to derived class in base constructor pastbin@gmail.com C++ 2 02-07-2008 02:41 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
Type casting a base class to a derived one? Frederic Rentsch Python 11 01-24-2007 07:55 PM
Casting from base to derived class James Brown C++ 7 06-27-2004 10:02 AM



Advertisments