Which option is better to use

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • themadme
    New Member
    • Mar 2007
    • 53

    Which option is better to use

    I have a private vector list in a class and I want to have a function that returns the vector. I could either say

    vector<int> getvectorlist()
    {
    return vectorlist;
    }

    or
    vector<int> *getvectorlist( )
    {
    return &vectorlist;
    }

    or

    vector<int> &getvectorlist( )
    {
    return vectorlist;
    }

    or just return the value with in the list

    int getdata( int Num )
    {
    vect iter = vectorlist.begi n()+Num;
    return (*vect );
    }

    where in main there would be a for loop calling that function.

    so which option is the best choice.

    Oh and im just wanting to know what data there is, not wanting to modify the list at all
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    My idea would be a class which contains the vector.
    Create a object instance and then using the object call the method which creates the vectorlist(That is update the member of the class).

    Then use the object to know the details of the vector


    Raghuram

    Comment

    • fual
      New Member
      • Feb 2008
      • 28

      #3
      Originally posted by themadme
      I have a private vector list in a class and I want to have a function that returns the vector. I could either say

      vector<int> getvectorlist()
      {
      return vectorlist;
      }

      or
      vector<int> *getvectorlist( )
      {
      return &vectorlist;
      }

      or

      vector<int> &getvectorlist( )
      {
      return vectorlist;
      }

      or just return the value with in the list

      int getdata( int Num )
      {
      vect iter = vectorlist.begi n()+Num;
      return (*vect );
      }
      [/
      where in main there would be a for loop calling that function.

      so which option is the best choice.

      Oh and im just wanting to know what data there is, not wanting to modify the list at all
      It all depends what you intend to do with this list, but if you just want to access the data without modifying it then the best thing might be:
      [code=cpp]const vector<int>& get_vector_list ( void ) const
      { return vectorlist; }[/code]
      That will prevent it from being modified. Alternatively you might want to provide access only to the "at" function and const_iterator "begin" and "end" functions.

      Comment

      • themadme
        New Member
        • Mar 2007
        • 53

        #4
        thanks for the reply's

        Comment

        Working...