2010-11-23 26 views
0

的子類,我有QTcpServer一個子類:重載Qt的功能在QTcpServer既可

的.h文件:

#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框架的基類有這個功能:

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

我用listen()函數重載了它。如果我這樣做,我不能調用上面的函數 - 在我看來這應該工作。爲什麼它不工作?有任何想法嗎?

回答

1

首先,只是一句話:那些QT類是爲構圖而設計的,而不是爲了繼承。

無論如何,這裏的問題在於你的listen()函數隱藏了基地的listen()

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

我主要通過繼承來提高我的技能 - 這只是一個大學項目。 所以我理解你的權利,有一個listen()函數在基類中不在文檔中?我只是不明白爲什麼listen()函數會覆蓋listen(const QHostAddress&address = QHostAddress :: Any,quint16 port = 0) - 函數。他們有完全不同的參數。 – Herrbert 2010-11-23 11:03:55

1

由於名稱listen具有相同的名稱隱藏該基地的功能:

你的問題是解決了。在你的班級的定義中,你可以寫出 using QTcpServer::listen;,因此該基地的聽將能夠參與超負荷分辨率