Thread: how would i pass on a vector pointer parameter

  1. #1
    Banned
    Join Date
    Nov 2005
    Posts
    8

    how would i pass on a vector pointer parameter

    just a simple question...

    i have this vector of a class
    Code:
    std::vector<PClass>* classes
    but on a function i need this as a parameter
    Code:
    createClass(const & std::vector<PClass *>)
    how could i pass my vector to this function?? would i do a long method in which creating a copy of vector of PClass pointers? or is there any other better solutions?

    thanks and more power..
    Last edited by bugmenot; 12-15-2005 at 02:41 AM.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Huh? Did you mean to put the * outside the vector's <> because that would be a pointer to a vector. If you didn't you can do this.
    Code:
    #include <iostream>
    #include <vector>
    
    class Pclass
    {
        int x;
    };
    
    void VecFunct(const std::vector<Pclass *> &vec)
    {
        //do stuff
    }
    
    int main()
    {
        std::vector<Pclass *> mainVec;
        
        VecFunct(mainVec);
        
        std::cin.get();
        
        return 0;
    }
    Woop?

  3. #3
    Banned
    Join Date
    Nov 2005
    Posts
    8
    No, i really mean it that way, that it is a pointer to a vector of pclass and i must somehow use it on a function with a parameter of vector of the class pointer.

    it's really confusing...

  4. #4
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Can't be done, your vectors contain two different things, and are not interchangable.

    Possibly, maybe you meant
    Code:
    std::vector<PClass*>* classes
    in that case you could pass it like
    Code:
    createClass(*classes)
    but the bottom line is that the thing inside the <brackets> must be the same.

    Edit: I just read the last line of your post... yes you will have to copy a pointer of the object in the vecto to a new vector.

    Edit 2: I thought I'd warn you to be careful with copying/using those pointers, because they could get out of sync with the original vector, for instance if you delete an elementof the original vector, the pointers will become invalid.
    Last edited by Darryl; 12-15-2005 at 08:54 AM.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    52
    Another thing to be aware of with vectors (and the possible reason the function takes a vector of pointers) is that in putting an item into a vector, you're copying it, which can be resource and/or time intensive if the objects are expensive to copy.

  6. #6
    Banned
    Join Date
    Nov 2005
    Posts
    8
    I see. Maybe that would be the reason it should be passed as vector of pointers to prevent copying. anyway if i still persists on copying i may find it difficult to debug if there would be problems.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-22-2007, 12:58 AM
  2. Passing "this" as function parameter
    By pgavigan in forum C++ Programming
    Replies: 15
    Last Post: 07-13-2007, 10:06 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Problems generating terrain with OpenGL
    By OnionKnight in forum Game Programming
    Replies: 8
    Last Post: 04-26-2007, 05:05 AM
  5. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM