My class contains a vector and upon instantiating it, I want to initialize its vector with some input vector:
class Test {
public:
Test(std::vector<int>& input_vec) : vec(input_vec) {};
std::vector<int> vec;
};
int main()
{
std::vector<int> a{1,2,3};
Test t(a);
}
I am passing a
to Test
's constructor by reference for efficiency-reasons in case the input vector is very large. I have two concerns in mind:
- Is it actually more efficient in my case to pass it by reference or is it redundant since I am only making a copy of it?
- Does it pose a memory risk that I pass a vector by reference and then take a copy of it?