I am a novice trying to understand some C++ code in the open-source game Simutrans. Specifically, this declaration (line 79 in this header file):
class env_t
{
public:
<snip>
/// if we are the server, we are at this port
/// @see network_init_server()
static const uint16 &server;
The context tells us this line concerns public data members of the class env_t
. The static
keyword tells us that all objects of class env_t will have the property server.
Server's type is uint16
(a 16-bit unsigned integer) and it is a const
ant (so it cannot be overwritten in normal circumstances).
The part that puzzles me is why there is an ampersand (&) at the beginning of the member's name. I have only previously come across the ampersand in bitwise arithmetic and as the "address of" operator. Neither of those uses fit here: there is no arithmetic and I do not see how you could define server as the address of server. Wouldn't that be telling the program that the only possible value of the variable is the variable's own memory address? That not only seems to be circular, but it seems to be inconsistent with the fact that the program can definitely use more than 16 bits of memory addresses for other purposes.
It has been difficult to search for explanations because the ampersand is also a search operator. But this article gives seven different uses of ampersands in C++ and none of them seem to fit here. It's not an expression nor a double ampersand. According to these S.O. answers, the ampersand can be used in a function template to indicate that a parameter must be of a reference type. But my example is declaring a data member, which is not a template, and indeed not any kind of function. And the type is clearly uint16, right? So what does the ampersand indicate here, please?
It should be noted that Simutrans is more than two decades old, before all platforms had the Standard Library, so its code is sometimes idiosyncratic. But I am sure the more relevant fact is that I'm a beginner at C++!