Overloading a Qt-function in a sub-class of QTcpServer
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(开发者_StackOverflow中文版)));
}
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?
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);
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
精彩评论