简体   繁体   English

使一个没有默认构造函数的类成为另一个类的私有属性

[英]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.

相关问题 如何在基础 class 中将默认构造函数设为私有? - How to make default constructor private in base class? 我可以在类体中默认私有构造函数吗? - Can I default a private constructor in the class body or not? 当我尝试为另一个 class 创建构造函数时,不存在 class“Vector 2”的默认构造函数 - No default constructor exists for class "Vector 2" when I am trying to make a constructor for another class 另一个类的移动构造函数内部的移动类(没有默认构造函数) - moving class (with no default constructor) inside the move constructor of another class 向量<class>带有私有构造函数</class> - vector<class> with private constructor 如何检查类是否具有默认构造函数(public,protected或private) - How to check if a class has a default constructor, either public, protected or private 通过定义默认的私有构造函数使类不可继承 - Making a class non-inheritable by defining default private constructor C ++类的默认构造函数调用另一个类的另一个默认构造函数 - C++ a default constructor of a class calling another default constructor of another class 如何在没有默认构造函数的其他类中创建模板类 - how to create template class in another class with out default constructor 如果我想用另一个构造 class,则“类不存在默认构造函数” - "No default constructor exists for class" if I want to construct a class with another one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM