简体   繁体   English

在C ++上通过引用传递值

[英]passing value by reference on C++

I am trying to understand what the arguments are for this method in a class from ns-3. 我试图了解ns-3中的类中此方法的参数。

void ns3::OnOffHelper::SetAttribute ( std::string name, const AttributeValue & value ) 无效ns3 :: OnOffHelper :: SetAttribute(std :: string name,const AttributeValue&value)

I was able to pass the first argument, but I am not sure what the second argument should be. 我能够通过第一个参数,但是我不确定第二个参数应该是什么。 Does it mean I have to pass a pointer value for the second argument? 这是否意味着我必须为第二个参数传递一个指针值?

Thanks, BZ 谢谢,BZ

No, pass it how you would pass a normal variable. 不,传递它的方式与传递普通变量的方式相同。 The difference is that the variable is passed by reference and not by value so any changes the function SetAttribute makes to value will still be visible once the function returns. 区别在于变量是通过引用而不是通过值传递的,因此一旦函数返回, SetAttribute函数对value所做的任何更改仍将可见。 In this case however, its const & which promises you that it won't change value , it will only use it's information. 但是,在这种情况下,它的const &会向您保证不会更改value ,只会使用它的信息。 This is usually done to prevent expensive copies (before C++11). 通常这样做是为了防止昂贵的复制(在C ++ 11之前)。

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

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