C++中如何實現(xiàn)回調(diào)的方法示例
前言
C++中使用class語法實現(xiàn)回調(diào)(當然,,舊式的C函數(shù)指針回調(diào)也是支持的)
比如,有人提供一個類庫 AfCopyFile,能夠提供文件拷貝的功能,而且能通知用戶當前的進度。。。
int DoCopy(const char* source, const char* dst, AfCopyFileListener* listener);
用戶只需要自己實現(xiàn)一個AfCopyFileListener對象,傳給這個函數(shù)就行。。。
class MainJob : public AfCopyFileListener{
int OnCopyProgress(long long total,
long long transfered){
}
}
把Listener對象傳過去
AfCopyFile af; af.DoCopy(source, dst, this);
回調(diào)機制的缺點:
無論是C語言的回調(diào)函數(shù),還是C++里的Listener,都有一個共同的缺點:
它使代碼邏輯變得難以閱讀。。
我們應盡量避免使用回調(diào)機制,最好采用單向的函數(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ù)用關鍵字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) // <- 這個就是上下文件對象
{
// 計算百分比
unsigned long long total = translate(TotalFileSize);
unsigned long long copied = translate(TotalBytesTransferred);
// 打印進度
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, // 上下文對象
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)
{
// 打印進度
int percent = (int) ( (transfered * 100 / total) );
printf("[用戶: %s], %s -> %s : 進度 %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;
}
總結
以上就是這篇文章的全部內(nèi)容,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對我們的支持。
欄 目:C語言
下一篇:c語言printf實現(xiàn)同一位置打印輸出的實例
本文標題:C++中如何實現(xiàn)回調(diào)的方法示例
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1139.html
您可能感興趣的文章
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對數(shù)怎么表達
- 04-02c語言沒有round函數(shù) round c語言
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 01-10如何判斷一個數(shù)是否為2的冪次方?若是,并判斷出來是多少次方
- 01-10深入理解C++中常見的關鍵字含義
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10如何判斷一個數(shù)是否為4的冪次方?若是,并判斷出來是多少次方
- 01-10如何查看進程實際的內(nèi)存占用情況詳解
- 01-10深入Main函數(shù)中的參數(shù)argc,argv的使用詳解


閱讀排行
本欄相關
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10C#中split用法實例總結


