-1

What does the following mean? I don't know what to search for as just searching : gives me nothing...

Server::Server(int port) : listen_sock(0), current_autogen_nickname(1)

where listen_sock used later in:

listen_sock = socket(AF_INET, SOCK_STREAM, 0);

and current_autogen_nickname not used.

Johnson Steward
  • 534
  • 3
  • 16
  • 1
    http://en.cppreference.com/w/cpp/language/initializer_list – tkausl Jan 16 '16 at 07:01
  • http://en.cppreference.com/w/cpp/language/initializer_list is the answer – Ed Heal Jan 16 '16 at 07:02
  • Great minds think alike – Ed Heal Jan 16 '16 at 07:02
  • yes you can search for it: http://stackoverflow.com/search?q=what+does+colon+in+constructor+mean , https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=c%2B%2B%20what%20does%20colon%20in%20constructor%20mean – bolov Jan 16 '16 at 07:10

1 Answers1

1

It means that you are defining a constructor for class Server which is declared with one int parameter. The class has fields listen_sock which is being set to 0 and then current_autogen_nickname which is being set to 1

You are basically defining a constructor and using an initializer list.

ucsunil
  • 7,378
  • 1
  • 27
  • 32
  • Then, why bother initializing `listen_sock` even if it will be assigned later? – Johnson Steward Jan 16 '16 at 07:09
  • 1
    Better to give everything a value when constructing. As code is organic best to get the initialisation list to do this at the start – Ed Heal Jan 16 '16 at 07:13
  • 1
    Plus one to what Ed Heal said. You don't have to do it. Just good practice. – ucsunil Jan 16 '16 at 07:20
  • Also put the initialisation list in the same order as they are declared – Ed Heal Jan 16 '16 at 07:25
  • @JohnsonSteward because someone writes bad code. It should have been initialized to the actual socket descriptor right in that initializer list, and should have thrown an exception if the socket call failed. – Cubbi Jan 16 '16 at 16:08