符号':',并在底线后面加上下划线. [英] symbol ':' and Underscores after the costructors.

查看:153
本文介绍了符号':',并在底线后面加上下划线.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我是c ++的新手,我想知道在类的构造函数中下划线后面跟变量名称是什么,我有一小段这样的代码


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


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));
}



在这里,我在contructor参数后面加上``:''后得到io_service_ 我不明白为什么在参数后面有一个符号:"以及该符号:"之后的含义实际上是什么.我也不知道该如何提及它们.请用简单的话帮助我,让我不知所措.


包含以上代码的整个类如下.




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.


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 */
};

推荐答案

下划线?它什么也没做,这只是变量/类型/字段/函数名称的有效部分.

如果在构造器server::server的主体之前和'':''之后是io_service_acceptor_new_connection_,则它们是字段初始化程序,同时也是thp.它们只是要在构造函数的主体启动之前根据C ++语法初始化为圆括号中的值的此类字段的名称.

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

此语法很重要,因为您可以通过这种方式初始化常量.您不能将赋值运算符应用于常量;参见 http://stackoverflow.com/questions/1423696/how-to -initialize-a-const-field-in-constructor [ ^ ].

有关您使用的特定Boost类的详细信息,请参考源代码的boost参考.

—SA
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


这篇关于符号':',并在底线后面加上下划线.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆