Like Tree1Likes
  • 1 Post By grumpy

passing a vector to a function

This is a discussion on passing a vector to a function within the C++ Programming forums, part of the General Programming Boards category; hi all In my program, I have a function which is supposed to manipulate the vectors passed to it. I ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    35

    Question passing a vector to a function

    hi all
    In my program, I have a function which is supposed to manipulate the vectors passed to it. I assume that if a vector is passed to a function, a copy of it is built and any change done in the body of the function would not affect the original vector(the changes are made to the copied version).
    therefore, I am trying to pass a reference of my vectors to my function. but how is it possible?
    by the way I've tried passing vector * to my function but it seems that I get stuck in getting them to work.

    my passed vector is :vector<Game*>* matches //Game is a defined class of mine

    1.how can I tell a "for" loop to count as many times as matches.size() ?
    I'm having trouble figuring out the syntax...like it'd be *(matches).size() or...what?

    2. I wanna access the attributes of my Game class through the vector. I don't know how to do this either.
    *(matches[k])->attribute or *(matches)[k]->attribute

    I'd appreciate it a lot if you help me out.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,590
    put the '*' inside the parentheses - (*matches) - or use the arrow operator -> on matches to access its members.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    35
    oh, thanks!
    by the way, I have a class named Player and a subclass named Winner which inherits from Player.
    I have this vector : vector<Winner*>* winners passed to my function.
    in the body of this function, I wanna pass this vector as a vector<Player*> to another function.
    how can I cast it? I'm just confused!
    something like this: dynamic_cast<vector<Player*>*>(winners)

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    2,590
    dynamic_cast is only for polymorphic types (derived classes and their parents). what I think you need here is just the address-of operator. when calling the function that accepts the vector pointer parameter, just put an '&' in front of the vector's name, and it will convert it to a pointer.

    Code:
    void SomeFunction(std::vector<Player*>* vPlayers);
    
    vector<Player*> players;
    
    SomeFunction(&players);
    another option is just to have the function take a reference as a parameter. if you don't want the function to modify the vector, you'll make it a const reference.

    Code:
    void CanChangeTheVector(std::vector<Player*>& vPlayers);
    void CannotChangeTheVector(const std::vector<Player*>& vPlayers);
    
    vector<Player*> players;
    
    CanChangeTheVector(players);
    CannotChangeTheVector(players);
    hope this helps

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,662
    Quote Originally Posted by Farnaz View Post
    by the way, I have a class named Player and a subclass named Winner which inherits from Player.
    I have this vector : vector<Winner*>* winners passed to my function.
    in the body of this function, I wanna pass this vector as a vector<Player*> to another function.
    how can I cast it? I'm just confused!
    something like this: dynamic_cast<vector<Player*>*>(winners)
    Yikes, no.

    What you need to do is create a vector<Player *>, copy all elements of the vector<Winner *> to it, and then pass the vector<Player *> on. This will work in your case as a pointer/reference to Winner can be implicitly converted to a pointer/reference to Player.

    Incidentally, rather than passing a vector<Winner *> * to your function, try passing a (possibly const) vector<Winner *> & instead.
    rags_to_riches likes this.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error while passing a reference of a vector to a function.
    By manasij7479 in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2011, 06:36 AM
  2. Passing Vector as a Reference
    By gbman88 in forum C++ Programming
    Replies: 8
    Last Post: 01-21-2011, 02:30 AM
  3. Passing Vector<string>
    By federico in forum C++ Programming
    Replies: 3
    Last Post: 10-12-2010, 04:00 AM
  4. passing std::vector and optimisation
    By l2u in forum C++ Programming
    Replies: 10
    Last Post: 07-03-2008, 12:01 PM
  5. passing a vector as an arguement
    By gL_nEwB in forum C++ Programming
    Replies: 7
    Last Post: 05-13-2006, 11:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21