Public access of std::vector or other big type

Oct 13, 2014 at 5:22pm
Couldn't really find any exact answer on google.

If I have a class like this

1
2
3
4
class A {
  public:
    std::vector<int> v;
};


Then I do
1
2
 A my_a;
/*do something with*/ my_a.v;


Does it automatically access v efficiently (by reference/memory location), or does public access cause a mutable copy to made? I suspect it's the former (efficient), but just wanted to be sure.

In other words, declaring v as private and doing
std::vector<int>& getV() {return v; } isn't any more efficient for a large vector than public accessing A.v?


Edit: Moreover, does this apply to any type, complicated or not?

1
2
3
4
5
6
7
8
9
template <typename T>
class B {
  public:
    T t;
};
int main() {
    B<some_big_type> b;
    b.t.dosomething(); // won't make a copy of t?
}
Last edited on Oct 13, 2014 at 5:29pm
Oct 13, 2014 at 5:38pm
In other words, declaring v as private and doing
std::vector<int>& getV() {return v; } isn't any more efficient for a large vector than public accessing A.v?

No.

Moreover, does this apply to any type, complicated or not?

Yes.
Oct 13, 2014 at 5:41pm
Edit: Still want one little clarification if you don't mind
Great, thanks. Gonna change my code to just make some T's public then (won't affect the "user" of the system of classes I have so efficiency was the only thing I was concerned with).

Edit: Err sorry, I had a double negative in that first question you quoted.

Just want to be sure you meant that making the vector public is just as efficient. (yes if true)
Thanks in advance.
Last edited on Oct 13, 2014 at 5:54pm
Oct 14, 2014 at 4:32am
Just want to be sure you meant that making the vector public is just as efficient. (yes if true)

Yes.
Topic archived. No new replies allowed.