[英]Passing class as a parameter c++
im new to programming and just start learning C++ a few weeks, im current doing the random number stuff, but i don't understand why the parameters sometime has "()" and sometime doesn't, hope someone can explain to me, thanks..我是编程新手,刚开始学习 C++ 几个星期,我目前正在做随机数的东西,但我不明白为什么参数有时有“()”有时没有,希望有人能给我解释一下,谢谢..
int main()
{
random_device rd;
mt19937 rdn(rd()); //Why this parameter needs "()"?
uniform_int_distribution<int> maxrd(1, 5000);
int n = maxrd(rdn); //And why this parameter doesn't need "()"?
cout << n;
};
mt19937 rdn(rd());
In the above statement, rd()
uses(calls) the overloaded std::random_device::operator()
and then the return value from that is used as an argument to mt19937
's constructor.在上述语句中,
rd()
使用(调用)重载的std::random_device::operator()
,然后将其返回值用作mt19937
构造函数的参数。
Basically the parenthesis ()
is used to call the operator()
of std::random_device
.基本上括号
()
用于调用std::random_device
的operator()
。 That is, here the parenthesis ()
after the rd
are there because we want to pass the returned value from rd()
as argument and not rd
itself.也就是说,这里
rd
后面的括号()
在那里,因为我们希望将rd()
的返回值作为参数而不是rd
本身传递。
int n = maxrd(rdn);
In the above statement, we're calling std::uniform_int_distribution::operator()
which takes a Generator
as argument and so we're passing rdn
as an argument since rdn
is already a generator.在上面的语句中,我们调用了
std::uniform_int_distribution::operator()
,它接受一个Generator
作为参数,因此我们将rdn
作为参数传递,因为rdn
已经是一个生成器。
Note here we're not using ()
after rdn
because we want to pass rdn
as argument and not the returned value from rdn()
.请注意,这里我们没有在
rdn
之后使用()
,因为我们希望将rdn
作为参数传递,而不是rdn()
的返回值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.