前言
新吳網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)公司,新吳網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為新吳上千提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的新吳做網(wǎng)站的公司定做!
C++中使用class語(yǔ)法實(shí)現(xiàn)回調(diào)(當(dāng)然,,舊式的C函數(shù)指針回調(diào)也是支持的)
比如,有人提供一個(gè)類庫(kù) AfCopyFile,能夠提供文件拷貝的功能,而且能通知用戶當(dāng)前的進(jìn)度。。。
int DoCopy(const char* source, const char* dst, AfCopyFileListener* listener);
用戶只需要自己實(shí)現(xiàn)一個(gè)AfCopyFileListener對(duì)象,傳給這個(gè)函數(shù)就行。。。
class MainJob : public AfCopyFileListener{ int OnCopyProgress(long long total, long long transfered){ } }
把Listener對(duì)象傳過去
AfCopyFile af; af.DoCopy(source, dst, this);
回調(diào)機(jī)制的缺點(diǎn):
無論是C語(yǔ)言的回調(diào)函數(shù),還是C++里的Listener,都有一個(gè)共同的缺點(diǎn):
它使代碼邏輯變得難以閱讀。。
我們應(yīng)盡量避免使用回調(diào)機(jī)制,最好采用單向的函數(shù)調(diào)用。
示例代碼:
AfCopyFile.h
#ifndef _AF_COPY_FILE_H #define _AF_COPY_FILE_H class AfCopyFile { public: // 作為內(nèi)部類 class Listener { public: virtual int OnCopyProgress(long long total, long long transfered) = 0; }; public: int DoCopy(const char* source, const char* dst, Listener* listener); }; #endif
AfCopyFile.cpp
#include <stdio.h> #include <Windows.h> #include "AfCopyFile.h" // 將LARGE_INTTEGER類型轉(zhuǎn)成unsigned long long inline unsigned long long translate(LARGE_INTEGER num) { unsigned long long result = num.HighPart; result <<= 32; result += num.LowPart; return result; } // 回調(diào)函數(shù) // 注:要求將此函數(shù)用關(guān)鍵字CALLBACK修飾(這是Windows API的要求) static DWORD CALLBACK CopyProgress( LARGE_INTEGER TotalFileSize, LARGE_INTEGER TotalBytesTransferred, LARGE_INTEGER StreamSize, LARGE_INTEGER StreamBytesTransferred, DWORD dwStreamNumber, DWORD dwCallbackReason, HANDLE hSourceFile, HANDLE hDestinationFile, LPVOID lpData) // <- 這個(gè)就是上下文件對(duì)象 { // 計(jì)算百分比 unsigned long long total = translate(TotalFileSize); unsigned long long copied = translate(TotalBytesTransferred); // 打印進(jìn)度 AfCopyFile::Listener* listener = (AfCopyFile::Listener*) lpData; listener->OnCopyProgress(total, copied); return PROGRESS_CONTINUE; } int AfCopyFile::DoCopy(const char* source, const char* dst, Listener* listener) { BOOL ret = CopyFileEx(source, dst, &CopyProgress, // 待回調(diào)的函數(shù) listener, // 上下文對(duì)象 NULL, 0); return ret ? 0 : -1; }
main.cpp
#include <stdio.h> #include <string.h> #include "AfCopyFile.h" class MainJob : public AfCopyFile::Listener { public: int DoJob() { strcpy(user, "shaofa"); strcpy(source, "c:\\test\\2.rmvb" ); strcpy(dst, "c:\\test\\2_copy.rmvb"); AfCopyFile af; af.DoCopy(source, dst, this); // 將this傳過去 return 0; } int OnCopyProgress(long long total, long long transfered) { // 打印進(jìn)度 int percent = (int) ( (transfered * 100 / total) ); printf("[用戶: %s], %s -> %s : 進(jìn)度 %d %%\n", user, source, dst, percent); return 0; } private: char source[256]; char dst[256]; char user[64]; }; int main() { MainJob job; job.DoJob(); return 0; }
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)創(chuàng)新互聯(lián)的支持。
網(wǎng)頁(yè)題目:C++中如何實(shí)現(xiàn)回調(diào)的方法示例
網(wǎng)址分享:http://chinadenli.net/article8/ppdjip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、ChatGPT、網(wǎng)站排名、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)