[英]Make a class with no default constructor a private property of another class
I am trying to make a class that manipulates with Boost sockets to make the conections simple to use. 我正在尝试创建一个使用Boost套接字操作的类,以使连接简单易用。
My SocketClient
class has a few properties with boost::asio::ip::tcp::socket
being one of them. 我的
SocketClient
类有一些属性,其中boost::asio::ip::tcp::socket
就是其中之一。 But I get C2512 error in my constructor, because boost::asio::ip::tcp::socket
cannot exist unitialised, as it has no constructor. 但是我的构造函数中出现C2512错误,因为
boost::asio::ip::tcp::socket
不能存在单元化,因为它没有构造函数。
Here, see the code of the class: 在这里,查看该类的代码:
class SocketClient {
private:
int port; //Port, currently unused
boost::asio::io_service io_service;
boost::asio::ip::tcp::resolver::iterator endpoint_iterator;
boost::asio::ip::tcp::socket sock; //This causes the error
//It wants to be like this (impossible too):
//boost::asio::ip::tcp::socket sock(io_service);
public:
void init(const char*, const char* );
SocketClient(); //Default constructor
bool connect();
bool read(int bytes, char *text);
bool send(int length, char *text);
bool send(std::string text);
unsigned int timeout;
};
And here is the constructor: 这是构造函数:
SocketClient::SocketClient() { //ERROR: (23): error C2512: 'boost::asio::basic_stream_socket<Protocol>' : no appropriate default constructor available
sock=boost::asio::ip::tcp::socket(io_service); //Adding this didn't help
}
So what to do? 那么该怎么办? Do I have to keep
sock
as void*
? 我必须保持
sock
void*
?
Use initialization lists : 使用初始化列表 :
SocketClient::SocketClient()
:
sock(io_service)
{
// Other initialization code here...
}
Be careful though: this is well-defined because member variables are constructed in the order they appear in the class definition , and io_service
appears before sock
. 但请注意:这是明确定义的,因为成员变量按它们在类定义中出现的顺序构造,而
io_service
出现在 sock
之前 。 If that wasn't the case, you would pass an uninitialized object to the constructor of socket
, most likely resulting in Undefined Behavior. 如果不是这种情况,则将未初始化的对象传递给
socket
的构造函数,最有可能导致未定义的行为。
If you are using C++ 11, you can do it like this: 如果您使用的是C ++ 11,可以这样做:
boost::asio::io_service io_service;
boost::asio::ip::tcp::resolver::iterator endpoint_iterator;
boost::asio::ip::tcp::socket sock { io_service };
But (as Andy Prowl said) io_service must be must be placed before sock in the member list. 但是(正如Andy Prowl所说)必须在成员列表中的袜子之前放置io_service。
This would compile, but may lead to unpredictable errors: 这会编译,但可能会导致不可预测的错误:
boost::asio::ip::tcp::socket sock { io_service };
boost::asio::io_service io_service;
generally - as already stated - whenever possible use initialization lists. 通常 - 如前所述 - 尽可能使用初始化列表。 also use a naming scheme that identifies members.
还使用标识成员的命名方案。
also already stated - initialization depends on member order. 也已经说过 - 初始化取决于成员的顺序。 if you have dependencies between members this is most often a sign for a design error.
如果您在成员之间存在依赖关系,那么这通常是设计错误的标志。 it smells.
它闻起来臭臭的。 at least document this well in the code - there is always a next maintainer.
至少在代码中记录这一点 - 总有下一个维护者。 of course you can alway hold elements via typed (smart) pointers.
当然,你总是可以通过键入(智能)指针来保存元素。
for the concrete problem I'd suggest to pass the io_service via reference. 对于具体问题,我建议通过引用传递io_service。 this would give you more control over the io_service (eg. use async mode and run multiple sockets within one io_service)
这将使您更好地控制io_service(例如,使用异步模式并在一个io_service中运行多个套接字)
#include <boost/asio.hpp>
class SocketClient {
private:
boost::asio::io_service& m_io_service;
boost::asio::ip::tcp::socket m_socket;
[...]
public:
SocketClient(boost::asio::io_service& io_service);
[...]
};
SocketClient::SocketClient(boost::asio::io_service& io_service)
: m_io_service(io_service)
, m_socket(io_service)
[...]
{
}
header .h 标题.h
boost::asio::ip::tcp::socket *sock;
body .cpp 身体.cpp
sock = new boost::asio::ip::tcp::socket(service);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.