C++ 使用boost::asio::io_服务作为类成员字段
我有使用boost asio库的课程: 标题:C++ 使用boost::asio::io_服务作为类成员字段,c++,boost,boost-asio,C++,Boost,Boost Asio,我有使用boost asio库的课程: 标题: class TestIOService { public: void makeConnection(); static TestIOService getInst(); private: TestIOService(std::string address); std::string address; // boost::asio::io_service service; }; Impl: 当我在make
class TestIOService {
public:
void makeConnection();
static TestIOService getInst();
private:
TestIOService(std::string address);
std::string address;
// boost::asio::io_service service;
};
Impl:
当我在makeConnection方法中使用此行定义服务时:
boost::asio::io_service service;
这没有问题,但当我将其作为类字段成员(在代码中注释掉)时,我得到以下错误:
注意:“TestIOService::TestIOService(TestIOService&&)”是隐式的
已删除,因为默认定义的格式不正确:
类测试服务{
io\u服务
不可复制
您可以通过将其包装在shared\u ptr
中来快速共享它,但您确实应该首先重新考虑设计
如果您的类需要可复制,那么它在逻辑上不包含io\u服务
对象
例如,以下示例确实创建了两个不共享连接的测试类实例:
#include <boost/asio.hpp>
#include <boost/make_shared.hpp>
#include <iostream>
class TestIOService {
public:
void makeConnection();
static TestIOService getInst();
private:
TestIOService(std::string address);
std::string address;
boost::shared_ptr<boost::asio::ip::udp::socket> socket;
boost::shared_ptr<boost::asio::io_service> service;
};
void TestIOService::makeConnection() {
using namespace boost::asio;
service = boost::make_shared<io_service>();
socket = boost::make_shared<ip::udp::socket>(*service);
socket->connect({ip::address::from_string("192.168.1.2"), 1234 });
//socket->close();
}
TestIOService::TestIOService(std::string address)
: address(address) { }
TestIOService TestIOService::getInst() {
return TestIOService("192.168.1.2");
}
int main() {
auto test1 = TestIOService::getInst();
auto test2 = TestIOService::getInst();
}
#包括
#包括
#包括
类测试服务{
公众:
void makeConnection();
静态测试服务getInst();
私人:
TestIOService(std::字符串地址);
std::字符串地址;
boost::共享的ptr套接字;
boost::共享的ptr服务;
};
void TestIOService::makeConnection(){
使用名称空间boost::asio;
service=boost::使_共享();
socket=boost::使_共享(*服务);
socket->connect({ip::address::from_string(“192.168.1.2”),1234});
//套接字->关闭();
}
TestIOService::TestIOService(std::字符串地址)
:地址(地址){}
TestIOService TestIOService::getInst(){
返回测试服务(“192.168.1.2”);
}
int main(){
auto test1=TestIOService::getInst();
auto test2=TestIOService::getInst();
}
谢谢你的回答,我不是很精通C++,但是关于那个设计,我应该为每个新的请求创建新的IoService吗?共享指针会做,就像你每次调用“代码> GESTSIME())/>代码一样。只是复制类不会创建新的IOIO服务(尽管注意从<代码> GET StUnter)返回/CODE >试图复制(或移动)“全班同学”)乔用一个有用的(?)示例该示例非常有用,它运行平稳,关于共享的\u ptr我需要对它们调用任何删除还是在堆栈上分配?:它们不在堆栈上分配,您不需要对它们调用任何删除。此外,大多数标准库功能都有文档,正如您所期望的:
boost::asio::io_service service;
#include <boost/asio.hpp>
#include <boost/make_shared.hpp>
#include <iostream>
class TestIOService {
public:
void makeConnection();
static TestIOService getInst();
private:
TestIOService(std::string address);
std::string address;
boost::shared_ptr<boost::asio::ip::udp::socket> socket;
boost::shared_ptr<boost::asio::io_service> service;
};
void TestIOService::makeConnection() {
using namespace boost::asio;
service = boost::make_shared<io_service>();
socket = boost::make_shared<ip::udp::socket>(*service);
socket->connect({ip::address::from_string("192.168.1.2"), 1234 });
//socket->close();
}
TestIOService::TestIOService(std::string address)
: address(address) { }
TestIOService TestIOService::getInst() {
return TestIOService("192.168.1.2");
}
int main() {
auto test1 = TestIOService::getInst();
auto test2 = TestIOService::getInst();
}