Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 721063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:49:49+00:00 2026-05-14T05:49:49+00:00

I took the following code from the examples page on Asio class tcp_connection :

  • 0

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

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()

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:49:50+00:00Added an answer on May 14, 2026 at 5:49 am

    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*/)
      {
      }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following code is to save image took from camera into photo album. if ([mediaType
I'm using backbone.js, underscore.js and jQuery. I took the following sample code from http://jsfiddle.net/thomas/C9wew/6/
While the following code is saving the image I took from my application into
I took the following code from http://blogs.msdn.com/b/jaredpar/archive/2008/05/16/switching-on-types.aspx static class TypeSwitch { public class CaseInfo
Running my script through Devel::NYTProf showed that the following portion of code took up
I took the rendered page from the SWT Browser and exported it to an
I took the example code from the Kendo UI demos at http://demos.kendoui.com/web/grid/remote-data.html , binding
In the code following, OP created a class with inheriting Template class class BatchRename(Template):
I took some code from my C# Windows form app which uses the Application
The following question is from a quiz I took a few weeks ago and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.