Splitting code into headers / source files

I took the following code on an Asio sample page

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

I am relatively new to C ++ (from C # background), and from what I understand, most people would split this into headers and source files (declaration / implementation, respectively). Is there a reason why I cannot just leave it in the header file if I intend to use it in many source files? If so, are there any tools that will automatically convert it to a declaration / implementation for me? Can someone show me what this would look like a header / source file separation for an example (or just parts of it, anyway)? I got confused in such strange things like this. typedef boost::shared_ptr<tcp_connection> pointer;Do I include this in the title or source? Same thing withtcp::socket& socket()

, -, ++.

+3
3

:

// 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; .

, . , , . , , , - . . , .

+1

, .

, , , , cpp.

. faq .

0

, . ACE (Adaptive Communication Framework) . .

0

Source: https://habr.com/ru/post/1739988/


All Articles