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

Discussion in 'C++' started by propokergrad@gmail.com, Mar 28, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. red floyd Guest

    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.
     
    red floyd, Mar 28, 2007
    #2
    1. Advertising

  3. Ian Collins Guest

    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.
     
    Ian Collins, Mar 28, 2007
    #3
  4. Guest

    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 <> wrote:
    > 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, 2007
    #4
  5. Ian Collins Guest

    wrote:

    Please don't top-post.
    >
    > On Mar 28, 6:37 pm, Ian Collins <> wrote:
    >
    >> 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.
     
    Ian Collins, Mar 30, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frederic Rentsch

    Type casting a base class to a derived one?

    Frederic Rentsch, Jan 11, 2007, in forum: Python
    Replies:
    11
    Views:
    1,331
    Chris Mellon
    Jan 24, 2007
  2. Replies:
    1
    Views:
    396
    myork
    May 23, 2007
  3. Replies:
    1
    Views:
    389
    Victor Bazarov
    May 23, 2007
  4. wangxiaohu
    Replies:
    10
    Views:
    678
    Tristan Wibberley
    Oct 21, 2007
  5. Replies:
    2
    Views:
    715
Loading...

Share This Page