C ++ Boost ASIO:在类内部初始化io_context: [英] C++ Boost ASIO: initializing io_context inside class:

查看:910
本文介绍了C ++ Boost ASIO:在类内部初始化io_context:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注Boost UDP多播发送器教程此处
。我将其修改为如下所示的类:

I am following Boost UDP multicast sender tutorial here . I modify it to make a class as follow:

#define _CRT_SECURE_NO_WARNINGS
#include <ctime>
#include <iostream>
#include <string>
#include <boost/array.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/asio.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/thread.hpp>
using boost::asio::ip::udp;
using std::cout;
using std::cin;
using std::endl;

class sender
{
private:
    boost::asio::io_context io_context;
    boost::asio::ip::udp::endpoint endpoint_;
    boost::asio::ip::udp::socket socket_;
    int message_count_;
    std::string message_;
    bool showBroadcast;

public:
    // constructor
    sender(std::string multicast_address, unsigned short multicast_port, bool show = true)
    {
        boost::asio::io_context io_context;
        boost::asio::ip::udp::endpoint endpoint_(boost::asio::ip::make_address("192.168.0.255"), 13000);
        boost::asio::ip::udp::socket socket_(io_context, endpoint_.protocol());
        socket_.set_option(boost::asio::ip::udp::socket::reuse_address(true));  // no need
    }

    // destructor
    ~sender()
    {
        cout << "UDP sender exiting." << endl;
    }

private:
    std::string get_input()
    {
        std::string result;
        cout << "Enter your message: ";
        getline(cin, result);
        return result;
    }

    std::string make_daytime_string()
    {
        using namespace std; // For time_t, time and ctime;
        time_t now = time(0);
        std::string result = ctime(&now);
        return result.erase(result.length() - 1, 1);
    }

    std::string some_string()
    {
        std::string result;
        result = make_daytime_string();
        return result;
    }
};

int main(int argc, char* argv[])
{
    try
    {
        sender s("192.168.0.255", 13000);
    }
    catch (std::exception& e)
    {
        std::cerr << "Exception: " << e.what() << "\n";
    }

    return 0;
}

我希望将io_context对象封装在类中,而不是在外部。 VC ++抱怨:

I wish to encapsulate io_context object inside the class, rather than having it outside. VC++ complains:

boost :: asio :: basic_datagram_socket':没有合适的默认构造函数

boost::asio::basic_datagram_socket': no appropriate default constructor available

我相信正试图迫使我按照以下方式构造构造函数(我尝试远离):

I believe it is trying to force me to have the constructor as follow (which I try to move away from):

    sender(boost::asio::io_context& io_context, const boost::asio::ip::address& multicast_address, unsigned short multicast_port, bool show = true) 
    : endpoint_(multicast_address, multicast_port),
    socket_(io_context, endpoint_.protocol())

如何将所有内容封装在类中?为什么Boost会强迫我采取其他方式?请帮忙。

How can I possibly have everything encapsulate inside my class? Why does Boost force me to do the other way? Please help. Thank you so much.

这似乎是由于io_context如建议的
。我希望可以复制此类。知道吗?

This seems to be due to io_context being non-copyable as suggested here . I wish to have this class copyable. Any idea?

推荐答案

没有一个ASIO类是可复制的,并且大多数都具有io_context引用,因此需要使用一个io_context引用进行构造。如果希望类是可复制的,则需要使用指向ASIO对象的共享指针。我不确定您为什么要复制ASIO对象,因为无论如何一次都不能在多个地方使用它们。

none of the ASIO classes are copyable and most hold an io_context reference so need to be constructed with one. If you want your classes to be copyable you need to use shared pointers to the ASIO objects. I'm not sure why you would want to copy ASIO objects though as you can't use them from more than one place at a time anyway.

这篇关于C ++ Boost ASIO:在类内部初始化io_context:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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