I’ve just been having a look at the boost asio framework and the examples. In the source code for Daytime.3 – An asynchronous TCP daytime server, the code defines a constructor for the tcp_server class as follows:
tcp_server(boost::asio::io_service& io_service) : acceptor_(io_service, tcp::endpoint(tcp::v4(), 50500))
I’m confused as to how the constructor is initializing the acceptor_ instance variable which is defined later on in the private section? I wanted to write equivalent code for this initialization within the body of the constructor (just for learning), but I can’t figure out how this ivar is being initialized.
Many thanks in advance for any help.
Uhm.. why shouldn’t it be able to do so? The member variables are visible through-out the whole class, no matter where they are defined:
If you meant how that while initialization thing works after the colon
:, search for “initializer list”/”ctor initializer” if you want the standard wording.Edit: Consider this class:
And now consider this function and other class:
You can initialize a member in the initializer list however you want, assuming the member has a fitting constructor. Now, for
constmember, you can only initialize them that way, because inside the ctor body, it can’t be assigned any more. You could only do that with non-const and non-reference variables:If this isn’t any help, I guess you really don’t express what you don’t understand.