Domanda

I am a noob and this is my first post.

With regards to the '&' character, I understand its general usage as a reference, address and logical operator,...

However, as I was trying to get the boost random number generator functions to work, I noticed another usage of the '&' character I have not seen before... nor is there explicit documentation about it(on the web at least).

Notice below the '&' comes at the END OF parameter mt19937&.

boost::variate_generator<boost::mt19937&, boost::uniform_int<> > die(gen, dist);

What is this? I am assuming it is being used as a reference somehow, but if I try to put the '&' at the front of the parameter, the compiler says it is unacceptable.

Any explanation would be appreciated.

NK.

È stato utile?

Soluzione

An ampersand before a variable gives you the address of that variable

int a = 3;
std::cout << &a << std::endl; // 0x12345678 or similar

An ampersand after a type makes that a reference type

int& b = a;
std::cout << b << std::endl; // 3
a = 4;
std::cout << b << std::endl; // 4

boost::mt19937 is a type, so boost::mt19937& is a reference type to boost::mt19937. Putting the ampersand before (&boost::mt19937) doesn't make sense because you cannot get the address of a type.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top