Passing class functions as arguments examples

From QT Wikipedia

Jump to: navigation, search

First thing i would like to recommend a must read article The Function Pointer Tutorials for general understanding. At this small article i just would like to add some little notices. If you want to define data type let say QMap based. First type would store pointer to function and second one pointer to object you would write like this:

 
typedef QMap<int, void (*)(void* pt2Object, QByteArray)> mapOperation;
typedef QMap<int, void (*)> mapOperationObject;
 

In C++ you use it like this.

 
mapOperation OperQuee;
 

Next thing you want to do is store some information you would do it like this. It's just dummy function:

 
void LhcWebServiceClient::LhcCheckLogin(QString username,QString passwd,void* pt2Object, void (*pt2Function)(void* pt2Object, QByteArray))
{
    QString searchStrin = "username="+username+"&login="+passwd;    
    QHttpHeader->setRequest("POST", *URLPostAddress+"index.php/xml/checklogin/");
 
    int operation;
    operation = QhttpClient->request(*QHttpHeader,searchStrin.toUtf8());
 
 
     this->OperQueeObject.insert(operation, pt2Object);
     this->OperQuee.insert(operation, pt2Function);
}
 

Let say in other part of code you want to reuse it based on key. Then you would write it like this:

 
void *pt2Object = (void*)this->OperQueeObject.take(<*key_to_use*>);
void (*pt2Function)(void *pt2Object, QByteArray) = static_cast< void ( *)(void *,QByteArray) > (this->OperQuee.take(<*key_to_use*>));            
QByteArray result = QhttpClient->readAll();
pt2Function(pt2Object,result);
 

So it's simple way to implement callback with this. Now to avoid using two list, one for object and another one for functions pointers, we can make struct. Notice that this time we use QPointer<QObject>. It's part of QT framework and used to prevent dangling pointer. Our structure would look like this.:

 
struct OperationQueStruc {
    void (*pt2Function)(void* pt2Object, QByteArray);
    QPointer<QObject> pt2Object;
};
 

Our QMap structure then would look like:

 
typedef QMap<int, OperationQueStruc> mapOperation;
 

And storing would look like this:

 
void LhcWebServiceClient::LhcCheckLogin(QString username, QString passwd, QObject* pt2Object, void (*pt2Function)(void* pt2Object, QByteArray))
{
 
    QString searchStrin = "username="+username+"&login="+passwd;    
    QHttpHeader->setRequest("POST", *URLPostAddress+"index.php/xml/checklogin/");
 
    int operation;
    operation = QhttpClient->request(*QHttpHeader,searchStrin.toUtf8());
 
    OperationQueStruc reqstruc;
    reqstruc.pt2Function = pt2Function;
    reqstruc.pt2Object = pt2Object;
 
    this->OperQuee.insert(operation, reqstruc);
}
 

And later usage would look like this:

 
QByteArray result = QhttpClient->readAll();            
OperationQueStruc reqstruc = static_cast< OperationQueStruc > (this->OperQuee.take(requestID));
 
// Associated with Guarded pointers, 
// if some object destroyed before request finishes.
if (reqstruc.pt2Object)
reqstruc.pt2Function(reqstruc.pt2Object,result);
 

So that's all for this time :) :)

Personal tools
Toolbox