小编Dor*_*oro的帖子

C++:返回NULL而不是struct

我有一个结构Foo.在伪代码中:

def FindFoo:
   foo = results of search
   foundFoo = true if a valid foo has been found  

   return foo if foundFoo else someErrorCode
Run Code Online (Sandbox Code Playgroud)

我怎样才能在C++中实现这一目标?

编辑删除了许多不准确之处.

c++ error-handling null-object-pattern

9
推荐指数
1
解决办法
1万
查看次数

拒绝到 TCP 服务器的传入连接

在我的程序中,我想设置 TCP 服务器的客户端限制。

目前我的传入连接代码是:

void TCPServer::incomingConnection(int handle)
{
    QPointer<TCPClient> client = new TCPClient(this);
    client->SetSocket(handle);

    clients[handle] = client;

    QObject::connect(client, SIGNAL(MessageRecieved(int,QString)), this, SLOT(MessageRecieved(int,QString)));
    QObject::connect(client, SIGNAL(ClientDisconnected(int)), this, SLOT(ClientDisconnected(int)));

    emit ClientConnected(handle);
}
Run Code Online (Sandbox Code Playgroud)

现在我想将客户端数量限制为例如 100 个活动连接总数。我是否必须以某种特殊的方式处理它,或者只是使用简单的if(clients.count() < 100)语句忽略它?

void TCPServer::incomingConnection(int handle)
{
    if(clients.count() < 100)
    {
        QPointer<TCPClient> client = new TCPClient(this);
        client->SetSocket(handle);

        clients[handle] = client;

        QObject::connect(client, SIGNAL(MessageRecieved(int,QString)), this, SLOT(MessageRecieved(int,QString)));
        QObject::connect(client, SIGNAL(ClientDisconnected(int)), this, SLOT(ClientDisconnected(int)));

        emit ClientConnected(handle);
    }
}
Run Code Online (Sandbox Code Playgroud)

这样做可以吗?未处理的连接是否处于活动状态(连接到服务器)但未在我的clients字典中列出?

qt tcp

5
推荐指数
1
解决办法
1895
查看次数

标签 统计

c++ ×1

error-handling ×1

null-object-pattern ×1

qt ×1

tcp ×1