0

I have a simple question. I have a class say A with a private member that is a stl container, for example a vector of ints. Is there a way to use its methods (e.g. size, resize, etc...) ? Does the classic "get function" suffice ?

Class A
{
private:
    std::vector<int> v;
public:
    std::vector<int> get_v() {return v;};
};

If yes, isn't a "get function" meant to only get the member and not to modify the member ?

Thank you very much

chiva
  • 535
  • 1
  • 4
  • 7
  • Basically you just declare the function as `const` and the return value as a `const` reference. – Captain Obvlious Feb 06 '14 at 15:53
  • I seem to have interpreted your question differently to others. Does your last question imply that you *want* to modify the member? Or do you not want that? – Joseph Mansfield Feb 06 '14 at 15:54
  • The main question is, if I want to modify the container, its size for example, how do I do that ? But I think Bathsheba solve the problem. I am going to check that immediately – chiva Feb 06 '14 at 16:10

3 Answers3

3

The normal thing to do here is to return a constant reference to the data member:

const std::vector<int>& get_v() const
{
    return v;
}

Note that I've also made the function constant. This tells you that the function will not modify any data members in the class.

Currently, you are taking a deep copy of the vector, which is expensive in terms of performance and also detaches you from the original data.

If you want to call methods like resize (that change the vector) then you can also supply a non-constant version of the function (overloading on const) is allowed in C++):

std::vector<int>& get_v()
{
    return v;
}

The compiler will call the const version if you have a const pointer (or reference) to an instance of A. Else if will call the non-const version.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Although, if you're going to provide a function to access it directly, you might as well give up the pretence of encapsulation and make it public. – Mike Seymour Feb 06 '14 at 16:05
0

That getter will return a copy of the internal vector. Any modifications you make will only affect the copy. That might be okay if you also provide a setter so that the modified copy can be passed back, so you could do something like this:

A a;
std::vector<int> v = a.get_v();
v.push_back(5);
a.set_v(v);

Alternatively, you can make the getter return a reference to the internal vector (so its return type would be std::vector<int>&, then you can modify it from outside.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
0

Your get method returns a copy of the data member. So such a method does not allow to modify the original vector,

If you want to provide an access to elements of the vector you can either overload operator[] or write a get method with a parameter.

For example

Class A
{
private:
    std::vector<int> v;
public:
    int operator []( std::vector<int>::size_type n ) const { return v[n]; }
    int & operator []( std::vector<int>::size_type n ) { return v[n]; }
};

or you can write get methods that do the same

Class A
{
private:
    std::vector<int> v;
public:
    int get( std::vector<int>::size_type n ) const { return v[n]; }
    int & get( std::vector<int>::size_type n ) { return v[n]; }
};

As for the idea to use a copy of the vector or a reference to it that to get its size then it is not a good idea. It is better to provide a method that returns the size of the vector. In this case the user interface will not depend on the type of the container.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335