将代码拆分为头文件/源文件 [英] Splitting Code into Headers/Source files

查看:187
本文介绍了将代码拆分为头文件/源文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Asio的示例页面中获取了以下代码

I took the following code from the examples page on Asio

    class tcp_connection : public boost::enable_shared_from_this<tcp_connection>
{
public:
  typedef boost::shared_ptr<tcp_connection> pointer;

  static pointer create(boost::asio::io_service& io_service)
  {
    return pointer(new tcp_connection(io_service));
  }

  tcp::socket& socket()
  {
    return socket_;
  }

  void start()
  {
    message_ = make_daytime_string();

    boost::asio::async_write(socket_, boost::asio::buffer(message_),
        boost::bind(&tcp_connection::handle_write, shared_from_this(),
          boost::asio::placeholders::error,
          boost::asio::placeholders::bytes_transferred));
  }

private:
  tcp_connection(boost::asio::io_service& io_service)
    : socket_(io_service)
  {
  }

  void handle_write(const boost::system::error_code& /*error*/,
      size_t /*bytes_transferred*/)
  {
  }

  tcp::socket socket_;
  std::string message_;
};

我对C ++相对较新(从C#背景开始),据我了解,大多数人会将其分为头文件和源文件(分别是声明/实现).如果要在许多源文件中使用它,是否有任何原因不能将其保留在头文件中?如果是这样,是否有任何工具可以自动将其转换为声明/实现?有人可以向我展示将其拆分为示例的标头/源文件的样子吗(或者只是一部分)?我对像这样的奇怪东西感到困惑typedef boost::shared_ptr<tcp_connection> pointer;是否在标头或源代码中包含它?与tcp::socket& socket()

I'm relatively new to C++ (from a C# background), and from what I understand, most people would split this into header and source files (declaration/implementation, respectively). Is there any reason I can't just leave it in the header file if I'm going to use it across many source files? If so, are there any tools that will automatically convert it to declaration/implementation for me? Can someone show me what this would look like split into header/source file for an example (or just part of it, anyway)? I get confused around weird stuff like thistypedef boost::shared_ptr<tcp_connection> pointer; Do I include this in the header or the source? Same with tcp::socket& socket()

我已经阅读了许多教程,但这一直使我对C ++感到困惑.

I've read many tutorials, but this has always been something that has confused me about C++.

推荐答案

拆分可能看起来像这样:

Splitting this could look like this:

// in the header-file
class tcp_connection : public boost::enable_shared_from_this<tcp_connection>
{
public:
  typedef boost::shared_ptr<tcp_connection> pointer;

  static pointer create(boost::asio::io_service& io_service);
  tcp::socket& socket();
  void start();
private:
  tcp_connection(boost::asio::io_service& io_service);
  void handle_write(const boost::system::error_code& /*error*/,
      size_t /*bytes_transferred*/);

  tcp::socket socket_;
  std::string message_;
};

// in the cpp-file

// #using the namespace in which the class was declared here

tcp_connection::pointer tcp_connection::create(boost::asio::io_service& io_service)
  {
    return pointer(new tcp_connection(io_service));
  }

  tcp::socket& tcp_connection::socket()
  {
    return socket_;
  }

  void tcp_connection::start()
  {
    message_ = make_daytime_string();

    boost::asio::async_write(socket_, boost::asio::buffer(message_),
        boost::bind(&tcp_connection::handle_write, shared_from_this(),
          boost::asio::placeholders::error,
          boost::asio::placeholders::bytes_transferred));
  }

  tcp_connection::tcp_connection(boost::asio::io_service& io_service)
    : socket_(io_service)
  {
  }

  void tcp_connection::handle_write(const boost::system::error_code& /*error*/,
      size_t /*bytes_transferred*/)
  {
  }

基本上,所有方法的实现都已移至cpp文件. typedef boost::shared_ptr<tcp_connection> pointer; 定义类型的别名并保留在标题中.

Basically, all the method implementations have been moved to the cpp-file. typedef boost::shared_ptr<tcp_connection> pointer; defines an alias for a type and remains in the header.

要了解拆分类的基本原理,请在此处此处.要了解不执行此操作的基本原理,请在此处.前面两个未拆分的原因是,通过这种方式,您不必链接任何东西即可使用该类.您只需要包含标题.希望能为您提供一个起点.

To understand the rationale of splitting classes, look here and here. To understand the rationale behind not doing it, look here. A reason for not splitting that follows from the previous two is, that this way you do not have to link anything to use the class. You only have to include the header. Hope that gives you a starting point.

这篇关于将代码拆分为头文件/源文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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