boost::asio::ip::tcp::socket& socket() const is returning _socket which is of type boost::asio::ip::tcp::socket is giving me this error
error: invalid initialization of non-const reference of type ‘boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> >&’ from a temporary of type ‘const boost::reference_wrapper<const boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > >’
socket() function is only getter and is not altering the state of this object so why does it fire error if I make it const ?
Because the function is returning a reference to an object. When the const member function is called, the returned reference must also be const.
boost::asio::ip::tcp::socket const& socket() constHowever, depending on what you want to do with that socket reference, you might have to return a non-const reference, in which case another option is to declare the getter function as non-const or declare the socket
mutablein the class definitionmutable boost::asio::ip::tcp::socket socket_