C++ 提升:在构建器之后初始化端点
C++ Boost: initializing endpoint after contructor
我尝试创建一个类来使用UDP。 我想在构造函数之后初始化端点,所以我修改它如下:
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 ============
};
显然,它不喜欢从原始构造函数中注释掉的这两行:
//endpoint(boost::asio::ip::address::from_string(multicast_address), multicast_port),
//socket(io_service, endpoint.protocol()),
它也不像新的构造函数:
UdpSender(boost::asio::io_service& io_service)
我能做些什么来使这一切成为可能? 我非常感谢您的帮助。 谢谢。
No.根据 Asio 参考,终结点对象应通过其构造函数进行配置。
但是,您可以通过 [smart-] 指针保存endpoint
,并在构造UdpSender
后创建它。
// 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++使用整数的压缩数组初始化对象
- 在 OpenCV 的 namedWindow 之前或之后初始化 Tesseract
- 在 C++20 之前和之后初始化 std::atomic
- 如何在初始化列表中的构造函数之后初始化变量/对象?
- 使用构造函数初始化结构还是在之后设置其值更好?
- C++ 提升:在构建器之后初始化端点
- 是在C 静态初始化之前或之后调用dllgetClassObject
- 从另一个线程调用shared_from_this但在初始化shared_ptr之后时获取bad_weak_ptr
- 如何在函数调用之后初始化变量,例如新表达式提供
- 是在默认构造函数之前或之后调用的初始化列表
- MFC和OpenGL CView,C ,Glew之后初始化OpenGL
- 在声明C++之后初始化常量变量
- C++编译器在 main 之后实现动态初始化
- 在malloc()之后初始化结构中对ptr的引用
- 在模板化类的typedef之后使用typename初始化函数时出错
- c++变量初始化在SWITCH语句中DEFAULT之后的CASE内
- 在构造函数之后初始化boost::asio套接字
- 不允许绑定到初始化列表中的引用的临时存在于actor结束之后的理由是什么?
- 类变量将在基类之后初始化