class base{
private:
int a;
char b;
public:
//functions to return a and b
}
class vect{
public:
vector<base> comp;
void add(int aa, char bb){ comp.pushback(base{aa,bb}); }
}
int main(){
vect a;
a.add{2, 'h'};
// here I want to access the elements, but not allow people in main
// to pushback manually or edit elements
a.comp[0].a = 5; // how to prevent this
a.comp.pushback(base{2,'k'}); // how to prevent this
cout << a.comp[0].a; // allow this (readonly)
}
How to achieve this? If I make the vector private and create a function in vect to return the whole vector won't it be a lot of transferring and can affect performance? Classes/names are arbitrary.
Then you can just call this function whenever you want to access the vector.
cout << a.getComp()[0].a;
If you want to access the vector multiple times and don't want to call getComp() each time you can use a variable (make sure you use a reference so that you don't create a copy).
1 2 3 4 5
const vector<base>& comp = a.getComp();
for (std::size_t i = 0; i < comp.size(); ++i)
{
cout << comp[i].a << '\n';
}
Be aware that if you store a reference to the internal vector you need to be careful not to use it after the vect object has been destroyed. You also need to be careful when adding new elements if you are storing references, pointers or iterators to the elements in the vector because the vector might have to reallocate (move the elements to a different location in memory).
Another approach is to have a function that takes an index as argument and returns an element in the vector. This hides the fact that the class uses a vector but you might need to add more functions if you need to know the number of elements and such.