Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C+中关闭析构函数中的连接+; 我正在考虑一个C++类,它管理我的TCP连接(Linux上)。破坏后,应关闭连接,如下所示: TCPConnection::~TCPConnection() { close(_socket); }_C++_Networking - Fatal编程技术网

在C+中关闭析构函数中的连接+; 我正在考虑一个C++类,它管理我的TCP连接(Linux上)。破坏后,应关闭连接,如下所示: TCPConnection::~TCPConnection() { close(_socket); }

在C+中关闭析构函数中的连接+; 我正在考虑一个C++类,它管理我的TCP连接(Linux上)。破坏后,应关闭连接,如下所示: TCPConnection::~TCPConnection() { close(_socket); },c++,networking,C++,Networking,问题是,当将此类对象放入例如向量时,连接也会像这样关闭,即使我仍然希望使用连接。我怎样才能解决这个问题?一般来说,这是一个坏主意吗?将tcp连接作为指针存储在std::vector中,而不是存储在实例中。然后,当您需要整理时,您可以删除指针 或者,如果您已经访问了std::shared_ptr,您可以将其存储在向量中,而当没有其他内容引用每个连接时,该连接将被删除。您想要实现的是一个名为RAII的设计模式,因此一旦您的TCPC连接被安装,它将获得资源,一旦它被销毁,它就会释放资源。若它被破坏了,

问题是,当将此类对象放入例如向量时,连接也会像这样关闭,即使我仍然希望使用连接。我怎样才能解决这个问题?一般来说,这是一个坏主意吗?

tcp连接作为指针存储在
std::vector
中,而不是存储在实例中。然后,当您需要整理时,您可以删除指针


或者,如果您已经访问了
std::shared_ptr
,您可以将其存储在
向量中,而当没有其他内容引用每个连接时,该连接将被删除。

您想要实现的是一个名为RAII的设计模式,因此一旦您的TCPC连接被安装,它将获得资源,一旦它被销毁,它就会释放资源。若它被破坏了,那个么这意味着程序员的意图是停止使用这些资源。若您仍然需要使用它们,那个么您必须延长对象的生命周期。您可以使用
typedef std::shared_ptr TCPConnectionPtr
,然后您可以将您的TCPConnectionPtr实例放在许多地方,并且只有在所有这些实例被销毁后,您的连接才会关闭


示例代码():

#包括
#包括
#包括
类TCP连接{
国际研究所;
静态int inst_计数;
公众:

TCPConnection(){inst=inst_count++;std::不能使类仅移动(不可复制),或通过指针使用它。这是一个好主意,它被称为
RAII
+1,用于尝试精简您的代码/C++实践。RAII使资源管理更容易、更安全。正如您所注意到的,RAII将资源的生存期与管理资源的句柄的生存期/作用域相链接。因此,当句柄的作用域结束时,资源如果您需要在不同的位置之间共享资源(即共享资源的所有权),您可以(应该)使用
std::shared_ptr
,它旨在安全地(以无泄漏的方式)共享资源的所有权。还有一种很好的
struct Foo{typedef std::shared_ptr;}这是一个很好的答案。但是考虑包含一个代码的例子,以使它对OP更加清楚。我添加了示例代码,希望对OP有用。
#include <iostream>
#include <vector>
#include <memory>

class TCPConnection {
    int inst;
    static int inst_count;
public:
    TCPConnection() { inst=inst_count++; std::cout << "TCPConnection created: " << inst << std::endl; }
    ~TCPConnection() { std::cout << "TCPConnection destroyed:" << inst << std::endl; }
};
int TCPConnection::inst_count;

// version if If c++11 is available, can be also implemented with boost::shared_ptr
// Removing individual TCPConnection from vector will also decrement its shared_ptr
// usage count and if it is zero then will destroy also such connections.
typedef std::shared_ptr<TCPConnection> TCPConnectionPtr;
typedef std::vector<TCPConnectionPtr> TCPConnectionPtrVec;

void fun1() {
    TCPConnectionPtrVec vec;
    vec.push_back(TCPConnectionPtr(new TCPConnection()));
}

// version for pre c++11 compiler, but I would recomend using boost::shared_ptr
// Class TCPConnectionsVecWrapper is a helper to make sure connections are safely freed.
class TCPConnectionsVecWrapper {
    // No copying allowed
    TCPConnectionsVecWrapper( const TCPConnectionsVecWrapper& );
    TCPConnectionsVecWrapper& operator=( const TCPConnectionsVecWrapper& );    

    typedef std::vector<TCPConnection*> TCPConnectionPtrsVec;    
    TCPConnectionPtrsVec vec;    
public:
    TCPConnectionsVecWrapper() {}
    ~TCPConnectionsVecWrapper() {
        for (TCPConnectionPtrsVec::const_iterator itr = vec.begin(); itr != vec.end(); ++itr) delete *itr;
    }

    TCPConnection* createConnection() {
        vec.push_back(new TCPConnection());
        return vec.back();
    }

    void remove(int index) {
        delete vec[index];
        vec.erase(vec.begin() + index);
    }

    TCPConnection* get(int index) { return vec[index]; }
    const TCPConnection* get(int index) const  { return vec[index]; }

    std::size_t size() const { return vec.size(); }        
};

void fun2() {
  // All TCPConnection will get deleted once tcpConnectionsWrapper is out of scope
  TCPConnectionsVecWrapper conns; 

  TCPConnection* con1 = conns.createConnection();
  (void)con1; // unused
  TCPConnection* con2 = conns.createConnection();
  (void)con2; // unused

  for ( size_t i = 0; i < conns.size(); ++i ) {
    TCPConnection* con = conns.get(i);
    (void)con; // unused
  }
  conns.remove(0);
}

int main(int argc, char** argv){ 
    fun1();
    fun2();
}