can I use base class pointer to swap the derived object in a vector?

Discussion in 'C++' started by John Black, Jul 2, 2004.

  1. John Black

    John Black Guest

    Hi,
    I have 2 vectors, vector<Derived_class*> vec1, vec2; can I do the
    following,

    for (int i=0; i<vec1.size(); ++i){
    for (int j=0; j<vec2.size(); ++j){
    if (.. some comparision of vec1 and vec2[j]...){
    Base_Class b = vec2;
    vec2 = vec2[j];
    vec2[j] = b;
    }
    }
    }

    Is there any problem here? In swapping I do not cast the pointer.
     
    John Black, Jul 2, 2004
    #1
    1. Advertising

  2. On Fri, 02 Jul 2004 10:41:34 -0600, John Black <> wrote:

    > Hi,
    > I have 2 vectors, vector<Derived_class*> vec1, vec2; can I do the
    > following,
    >
    > for (int i=0; i<vec1.size(); ++i){
    > for (int j=0; j<vec2.size(); ++j){
    > if (.. some comparision of vec1 and vec2[j]...){
    > Base_Class b = vec2;
    > vec2 = vec2[j];
    > vec2[j] = b;
    > }
    > }
    > }
    >
    > Is there any problem here? In swapping I do not cast the pointer.
    >


    You question doesn't make a lot of sense. The code you quote doesn't
    compile, so no you can't do that. But why not do it like this?


    if (.. some comparision of vec1 and vec2[j]...){
    Derived_class* b = vec2;
    vec2 = vec2[j];
    vec2[j] = b;
    }

    It seems the obvious thing to do.

    john
     
    John Harrison, Jul 2, 2004
    #2
    1. Advertising

  3. John Black

    Howard Guest

    "John Black" <> wrote in message
    news:...
    > Hi,
    > I have 2 vectors, vector<Derived_class*> vec1, vec2; can I do the
    > following,
    >
    > for (int i=0; i<vec1.size(); ++i){
    > for (int j=0; j<vec2.size(); ++j){
    > if (.. some comparision of vec1 and vec2[j]...){
    > Base_Class b = vec2;


    I take it you mean

    Base_Class* b = vec2;

    ?

    > vec2 = vec2[j];
    > vec2[j] = b;
    > }
    > }
    > }
    >
    > Is there any problem here? In swapping I do not cast the pointer.
    >


    Assuming that's what you mean, it's fine.

    -Howard
     
    Howard, Jul 2, 2004
    #3
  4. Re: can I use base class pointer to swap the derived object in avector?

    Howard wrote:
    > "John Black" <> wrote in message
    > news:...
    >
    >>Hi,
    >> I have 2 vectors, vector<Derived_class*> vec1, vec2; can I do the
    >>following,
    >>
    >> for (int i=0; i<vec1.size(); ++i){
    >> for (int j=0; j<vec2.size(); ++j){
    >> if (.. some comparision of vec1 and vec2[j]...){
    >> Base_Class b = vec2;

    >
    >
    > I take it you mean
    >
    > Base_Class* b = vec2;
    >
    > ?
    >
    >
    >> vec2 = vec2[j];
    >> vec2[j] = b;
    >> }
    >> }
    >> }
    >>
    >> Is there any problem here? In swapping I do not cast the pointer.
    >>

    >
    >
    > Assuming that's what you mean, it's fine.


    Somehow I think that conversion between Base_Class* (which is what
    'b' is) and Derived_class* (which is what 'vec2[j]' is), requires
    a cast (a static_cast, for example).

    But, I am still not sure why not use 'std::swap'?

    std::swap(vec2, vec2[j]);

    all type considerations are hidden and taken care of.

    Victor
     
    Victor Bazarov, Jul 2, 2004
    #4
    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. tirath
    Replies:
    3
    Views:
    720
    Ivan Vecerina
    Oct 12, 2003
  2. Replies:
    1
    Views:
    1,339
    Jonathan Mcdougall
    Dec 24, 2004
  3. Replies:
    0
    Views:
    525
  4. Replies:
    1
    Views:
    407
    myork
    May 23, 2007
  5. Replies:
    1
    Views:
    398
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page