c++回调函数

工具vs2008

四个文件:

基类文件: Config.h Config.cpp

子类文件:Profile.h Profile.cpp

主文件:boost.cpp


Config.h:

#include "stdafx.h"
#include <iostream>
using namespace std;
class AbstractProfile
{
private:
string str;
public:
virtual bool onSectionBegin(const char* sectionName) = 0;
public:
bool loadFromFile(const char* fileName);




};

Config.cpp:

#include "stdafx.h"
#include "Config.h"


bool AbstractProfile::loadFromFile(const char* fileName)
{


cout<<"AbstractProfile::loadFromFile fileName ="<<fileName<<endl;
if(onSectionBegin(fileName))
{
cout<<"回调成功!!"<<endl;
}
return true;
}


Profile.h:

#include "stdafx.h"
#include "Config.h"
#include <iostream>
using namespace std;
class Profile:AbstractProfile
{
public:
bool onSectionBegin(const char* sectionName);
bool loadFromFile(const char* fileName);
};


Profile.cpp


#include "stdafx.h"
#include "Profile.h"


bool Profile::onSectionBegin(const char* sectionName)
{
cout<<"Profile::onSectionBegin"<<endl;
return true;
}
bool Profile::loadFromFile(const char* fileName)
{
cout<<"Profile::loadFromFile"<<endl;
return AbstractProfile::loadFromFile(fileName);


}


boost.cpp

#include "Profile.h"

int _tmain(int argc, _TCHAR* argv[])

Profile pro;


pro.loadFromFile("11.txt");

system("pause"); 
return 0;
}


虚函数调用自己没有实现的函数,在子类中返回父类实现的函数。


运行结果:

Profile::loadFromFile

AbstractProfile::loadFromfile 

Profile::onsectionBeigin

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值