2010-11-23 34 views
0

のサブクラスでQtの機能のオーバーロード:私は<a href="http://doc.qt.io/archives/qt-4.7/qtcpserver.html" rel="nofollow">QTcpServer</a>のサブクラスを持って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()を隠しているという問題があります。名前listenは、同じ名前を持つベースの機能が非表示になりますので

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

私は自分のスキルをリフレッシュするために主にそれを行う - それは大学の単なるプロジェクトです。 ドキュメントに含まれていないベースクラスにlisten()関数があることは間違いありませんか?私はlisten()関数がlisten(const QHostAddress&address = QHostAddress :: Any、quint16 port = 0)関数を上書きする理由を理解できません。彼らは全く異なるパラメータを持っています。 – Herrbert

+0

いいえ、それはまさにあなたが隠している署名 'listen(const QHostAddress&、quint)'を持つ関数です。 C++のこの「機能」は、標準(ref $ 3.3.10)に記述されています。 – Simone

1

あなたの問題をすることで解決されます。あなたのクラスの定義では、 using QTcpServer::listen;と書くことができます。ベースのリスナーは、過負荷の解決に参加することができます

関連する問題

 関連する問題