繁体   English   中英

如何实现在C ++中通过引用传递的参数?

[英]how to fulfill a parameter that passed by reference in c++?

我创建了一个通过引用接受参数的方法,如下所示:

void Func1(QList<int> *a){
 (*a) << getDataFromAnotherFunction();
//or
(*a).append(getDataFromAnotherFunction());
}

QList<int> getDataFromAnotherFunction(){

//we will do something good over here

return QList<int>
}

但是问题是当我想使用a的数据时,其中没有数据。 它说0; 说我想计算其中的元素,如下所示:

//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

我应该如何传递参数以获得正确的数据?

问候。

您可能想要执行以下操作:

void Func1(QList<int> &a){  // this is pass-by-reference
a << getDataFromAnotherFunction(); 
}

然后像这样使用它:

QList <int> my_qlist;
Func1(my_qlist);

请注意,您必须重载<<运算符。 根据您所写的内容,当前的行为取决于以下事实:在getDataFromAnotherFunction中,您将返回一个空的QList。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM