C ++ Boost:在构造函数之后初始化端点 [英] C++ Boost: initializing endpoint after contructor

查看:136
本文介绍了C ++ Boost:在构造函数之后初始化端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试制作一个使用UDP的类.我想在构造函数之后初始化端点,因此我将其修改如下:

I try to make a class to use UDP. I want to have the endpoint initialized after the constructor, so I modify it as follows:

class UdpSender
{
private:
    boost::asio::ip::udp::endpoint endpoint;
    boost::asio::ip::udp::socket socket;

    string multicast_address;
    unsigned short multicast_port;
    boost::thread_group threads;    // thread group
    boost::thread* thread_main;     // main thread
    boost::thread* thread_listen;   // listen thread
    boost::thread* thread_getsend;  // get/send thread
    boost::mutex stopMutex;
    bool initialize = false;
    bool stop, showBroadcast;
    int i_getsend, i_listen, i_main, i_message, interval;
    string message;
public:
    // constructor
    UdpSender(boost::asio::io_service& io_service, std::string multicast_address, unsigned short multicast_port, int interval, bool show = false)
        : 
        //endpoint(boost::asio::ip::address::from_string(multicast_address), multicast_port),
        //socket(io_service, endpoint.protocol()),
        multicast_address(multicast_address),
        multicast_port(multicast_port),
        interval(interval),
        showBroadcast(show)
    {
        initialize = true;
        Initialize(io_service);
    }

    UdpSender(boost::asio::io_service& io_service)
    {
        initialize = true;
        Initialize();
    }
    // destructor
    ~UdpSender()
    {
        // show exit message
        cout << "Exiting UDP Communicator." << endl;
    }

    // initialize
    void Initialize(boost::asio::io_service& io_service)
    {
        if (initialize == false)
        {
            GetInfo();
        }

        boost::asio::ip::udp::endpoint endpoint(boost::asio::ip::make_address(multicast_address), multicast_port);
        boost::asio::ip::udp::socket socket(io_service, endpoint.protocol());
        socket.set_option(boost::asio::ip::udp::socket::reuse_address(true));   // no need

        thread_main = new boost::thread(boost::ref(*this));
        //thread_listen = new boost::thread(&UdpSender::Callable_Listen, this, interval, boost::ref(i_listen));
        //threads.add_thread(thread_listen);    // listen thread
        thread_getsend = new boost::thread(&UdpSender::Callable_GetSend, this, interval, boost::ref(i_listen), boost::ref(message));
        threads.add_thread(thread_getsend); // get/send thread
        stop = false;
        i_getsend = 0;
        i_listen = 0;
        i_main = 0;
        i_message = 0;
        message.clear();

        initialize = true;
    }
    // ============ other things removed to shorten code ============
};    

显然,它不喜欢从原始构造函数中注释掉的这两行:

Apparently, it doesn't like these two lines commented out from the original constructor:

//endpoint(boost::asio::ip::address::from_string(multicast_address), multicast_port),
//socket(io_service, endpoint.protocol()),

它也不像新的构造函数那样:

Nor does it like the new constructor:

UdpSender(boost::asio::io_service& io_service)

我该怎么做才能使所有这些变为可能?非常感谢您的帮助.谢谢.

What can I do to make all this possible? I really appreciate your help. Thanks.

推荐答案

否. 根据Asio参考,端点对象应通过其进行配置构造函数.

No. According to the Asio reference, the endpoint object should be configured via its constructors.

但是,您可以通过[smart-]指针按住endpoint,并在构造UdpSender之后创建它.

However, you could hold the endpoint by [smart-]pointer, and create it after UdpSender construction.

// defining:
private:
    boost::shared_ptr<boost::asio::ip::udp::endpoint> endpoint;

// creating in Initialize()
endpoint = boost::make_shared<boost::asio::ip::udp::endpoint>(boost::asio::ip::address::from_string(multicast_address), multicast_port);

这篇关于C ++ Boost:在构造函数之后初始化端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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