-1

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 constant (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++!

user4581301
  • 33,082
  • 7
  • 33
  • 54
Matthew
  • 156
  • 1
  • 9
  • Read more: https://en.cppreference.com/w/cpp/language/reference – jtbandes Aug 28 '20 at 21:00
  • This line is also important: https://github.com/aburch/simutrans/blob/a585bba61ecb09caf2fbd108bbb1bed4d476e76e/dataobj/environment.cc#L36 – jtbandes Aug 28 '20 at 21:01
  • See [this question](https://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in) for more details. – SuperStormer Aug 28 '20 at 21:03
  • 3
    As you can see, googling common C++ syntax is difficult. In general, the Internet is a bad place to learn C++ because the ratio of bad information to good information is so high that'ts dumb luck when when you stumble across good information without already knowing most of what you're looking for. What you want at this point of your C++ career is [a good textbook and maybe a few good language references](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). They will dramatically increase the speed at which you will come to understand the language. – user4581301 Aug 28 '20 at 21:15
  • 2
    No, inside a class definition, [`static`](https://en.cppreference.com/w/cpp/language/static) does *not* mean all objects have the member `server`. In fact, it means that *no objects* have that member, but rather that there is a global variable named `env_t::server`. – G. Sliepen Aug 28 '20 at 21:18
  • @user4581301 It doesn't seem very difficult considering the OP has linked the answer to his question twice. The first one has the answer in the very first use case and the second one straight up gives the answer. I just point out the irony of him putting so much effort into asking a question when he all he has to do is _read_ a couple of paragraphs. –  Aug 28 '20 at 21:44
  • 1
    @para you left out a couple words: "has to do is *read and understand* a couple of paragraphs" Understanding is the tricky part, and usually it can only be done when the material it builds on is also understood. – user4581301 Aug 28 '20 at 21:51

1 Answers1

0

That notation indicates that server is a reference. This has enough similarities to pointers that if you are comfortable with them, you can think of them as a very restricted pointer. If you aren't so comfortable with pointers, a reference is not a variable that holds onto its own data. Rather, it is a reference to another variable. This notation indicates that server is actually referring to data somewhere else, most likely outside of your env_t class.

This is typically done for two reasons. One is that if you change this value, it changes the value of the actual variable being referred to, so that if another piece of code looks at its value, they'll see your change. Vice versa, if someone else changes the value of the variable referenced by server, and you query the value of server, you'll see their changes.

Cort Ammon
  • 10,221
  • 31
  • 45