how to fulfill a parameter that passed by reference in c++?
I've created a method which is accept parameter by reference like following :
void Func1(QList<int> *a){
(*a) << getDataFromAnotherFunction();
//or
(*a).append(getDataFromAnotherFunction());
}
QList<int> getDataFromAnotherFunction(){
//we will 开发者_开发百科do something good over here
return QList<int>
}
but the problme is when I want to use the a's data there is no data in it. and it says 0; say I want to count the element in it like following :
//for passing a to func1 I use something like that
//QList a;
//func(&a);
//after retruning from func1 now I want to use the result like below :
a.size();
//but the answer is 0
how should I pass my parameter to get the correct data?
regards.
You probably want to do like the following:
void Func1(QList<int> &a){ // this is pass-by-reference
a << getDataFromAnotherFunction();
}
and then use it like this:
QList <int> my_qlist;
Func1(my_qlist);
Note that you must overload the << operator. Your current behaviour depends, from what you have written, on the fact that in getDataFromAnotherFunction you are returning an empty QList.
精彩评论