I have the following code in one class:
class A
{
std::vector<Parameter*> params;
std::vector<Parameter*> getParamVector() {return params;}
void addOneParam(Parameter* param)
{
params.push_back(param);
}
}
In another class, class B, I try to add items into the params vector by doing this:
classA_Ptr->getParamVector().push_back(aParamPtr);
But the params vector's size still 0, after above call.
I have to add above addOneParams(Parameter* param)
method to add items into the params vector. Why?
getParamVector()
returns a copy of params
. So when you push_back
onto it, you're adding to a temporary vector
that gets deleted right away. That in no way affects params
's size.
If you want to be able to do this via getParamVector()
, you'll have to return a reference to params
instead:
std::vector<Parameter*>& getParamVector() {return params;}
^^^
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments