在 QTcpServer 的子类中重载 Qt 函数

发布于 2024-10-04 16:04:24 字数 1217 浏览 2 评论 0原文

我有一个 QTcpServer 的子类:

.h-file:

#ifndef GEOLISTENER_H
#define GEOLISTENER_H

#include <QTcpServer>

class GeoListener : public QTcpServer
{
    Q_OBJECT
public:
    explicit GeoListener(QObject *parent = 0);
    bool listen(void);
signals:

public slots:
    void handleConnection(void);

};

#endif // GEOLISTENER_H

.cpp-文件:

#include "geolistener.h"
#include <QDebug>

GeoListener::GeoListener(QObject *parent) :
    QTcpServer(parent)
{
    QObject::connect(this, SIGNAL(newConnection()),this, SLOT(handleConnection()));
}

bool GeoListener::listen(void)
{
    bool ret;
    ret = this->listen(QHostAddress::Any, 9871); //This function isn't found!
    /* If something is to be done right after listen starts */

    return ret;
}

void GeoListener::handleConnection(void) {
    qDebug() << "got connection";
}

Qt-Framework 的基类具有此功能:

bool QTcpServer::listen ( const QHostAddress & address = QHostAddress::Any, quint16 port = 0 )

我用listen() 函数重载了它。如果我这样做,我就无法调用上面的函数 - 在我看来这应该可行。为什么它不起作用?有什么想法吗?

I have a subclass of QTcpServer:

.h-file:

#ifndef GEOLISTENER_H
#define GEOLISTENER_H

#include <QTcpServer>

class GeoListener : public QTcpServer
{
    Q_OBJECT
public:
    explicit GeoListener(QObject *parent = 0);
    bool listen(void);
signals:

public slots:
    void handleConnection(void);

};

#endif // GEOLISTENER_H

.cpp-file:

#include "geolistener.h"
#include <QDebug>

GeoListener::GeoListener(QObject *parent) :
    QTcpServer(parent)
{
    QObject::connect(this, SIGNAL(newConnection()),this, SLOT(handleConnection()));
}

bool GeoListener::listen(void)
{
    bool ret;
    ret = this->listen(QHostAddress::Any, 9871); //This function isn't found!
    /* If something is to be done right after listen starts */

    return ret;
}

void GeoListener::handleConnection(void) {
    qDebug() << "got connection";
}

The base class of the Qt-Framework has this function:

bool QTcpServer::listen ( const QHostAddress & address = QHostAddress::Any, quint16 port = 0 )

I overloaded it with a listen()-function. If I do that I can't call the function above - in my opinion this should work. Why isn't it working? Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘寄 2024-10-11 16:04:25

首先,请注意:那些 QT 类是为组合而设计的,而不是为继承而设计的。

无论如何,这里的问题是您的 listen() 函数隐藏了基础的 listen()

您的问题可以通过以下方式解决:

static_cast<QTcpServer*>(this)->listen(QHostAddress::Any, 9871);

First of all, just a remark: those QT classes are designed for composition, not for inheritance.

Anyway, the problem here is that your listen() function is hiding the base's listen().

Your problem is resolved by:

static_cast<QTcpServer*>(this)->listen(QHostAddress::Any, 9871);
掐死时间 2024-10-11 16:04:25

因为名称 listen 隐藏了基础的同名函数。在你的类的定义中你可以写
using QTcpServer::listen; 因此基础的监听将能够参与重载决策

Because the name listen hides the base's function with the same name. In the definition of your class you can write
using QTcpServer::listen; and thus the base's listen will be able to participate in overload resolution

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文