Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,
I am new to c++ and i would like to know what does the underscores followed by the vaiables name in a constructor of the class, i have a small piece of code like this


C#
server::server(ba::io_service& io_service, int thnum, int port)
    : io_service_(io_service),
      acceptor_(io_service_, ba::ip::tcp::endpoint(ba::ip::tcp::v4(), port)),
      new_connection_(connection::create(io_service_)),
      thp(thnum) {
    // start acceptor in async mode
    acceptor_.async_accept(new_connection_->socket(),
                           boost::bind(&server::handle_accept, this,
                                       ba::placeholders::error));
}


here i have io_service_ after the contructor arguments followed by the ':'
i dont understand any of this why there is a symbol ':' after the arguments and what are those following that symbol ':' actually means .I dont know how to mention them too. pls help me with your simple word to make me undestand.


the entire class which contains the above code is as follows.


C#
class server : private boost::noncopyable {
public:
    server(ba::io_service& io_service, int thnum, int port=10001);

private:
    void handle_accept(const boost::system::error_code& e);

    ba::io_service& io_service_;         /**< reference to io_service */
    ba::ip::tcp::acceptor acceptor_;     /**< object, that accepts new connections */
    connection::pointer new_connection_; /**< pointer to connection, that will proceed next */
    tp::pool thp;                        /**< thread pool object */
};
Posted
Updated 26-Oct-11 1:48am
v2

1 solution

The underscore? It does not do anything, this is just a valid part of the variable/type/field/function name.

If you mean io_service_, acceptor_ and new_connection_ before the body of the constructor server::server and after ':' — they are field initializers, as well as thp. They are just the names of this class fields to be initialized to the values in round brackets according to C++ syntax before the body of the constructor is started.

See http://www.cprogramming.com/tutorial/initialization-lists-c++.html[^].

This syntax is important because in this way you can initialize a constant; you cannot apply an assignment operator to a constant; see http://stackoverflow.com/questions/1423696/how-to-initialize-a-const-field-in-constructor[^].

For the detail of particular boost classes you use please refer to boost reference of source code.

—SA
 
Share this answer
 
v5
Comments
Simon Bang Terkildsen 25-Oct-11 22:18pm    
+5
Sergey Alexandrovich Kryukov 25-Oct-11 23:22pm    
Thank you, Simon.
--SA
Espen Harlinn 26-Oct-11 15:21pm    
5'ed!
Sergey Alexandrovich Kryukov 26-Oct-11 15:37pm    
Thank you, Espen.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900