一:设置一个函数指针,在需要使用的时候调用
{
callback(pURL,true);
}
{
std::cout<<"onDownloadFinished..."<<pURL<<" status:"<<bOK<<std::endl;
}
{
DownLoadFile("http://wwww.baidu.com",onDownloadFinished);
return 0;
}
{
public:
virtual void OnDownloadFinished(const char *pURL,bool bOK) = 0;
};
class CMyDownloader
{
public:
CMyDownloader (IDownloadSink *pSink)
:m_pSink(pSink)
{
}
void DownloadFile(const char* pURL)
{
std::cout<<"downloading..."<<pURL<<""<<std::endl;
if(m_pSink!=NULL)
{
m_pSink->OnDownloadFinished(pURL,true);
}
}
private:
IDownloadSink *m_pSink;
};
class CMyFile:public IDownloadSink
{
public:
void download()
{
CMyDownloader downloader(this);
downloader.DownloadFile("www.baidu.com");
}
virtual void OnDownloadFinished(const char *pURL,bool bOK)
{
std::cout<<"onDownloadFinished..."<<pURL<<" status:"<<bOK<<std::endl;
}
};
void main()
{
CMyFile *file = new CMyFile();
file->download();
system("pause");
}