#include <iostream>
#include <list>
using namespace std;
/*包装类,成员函数指针类型与普通函数指针类型不同,不能在类外直接定义*/
template <class T, class _Ty>
class CPtrMaker
{
public:
//自定义成员函数指针类型
typedef void (T::*FuncPtr)(const list<_Ty>&);
};
/*每一段长度*/
const int PER_REPLY_LEN = 5;
/*
*1、实现容器按照指定长度分段,长度不足则按实际长度
*2、根据传入的函数指针,显示数据
*/
template <class T, class _Ty>
void ReplyInfor(const list<_Ty>& inforIn, void (T::*FuncPtr)(const list<_Ty>&))
{
if(inforIn.size()== 0)
return;
list<_Ty> listCopy;
list<_Ty>::const_iterator listIter;
list<_Ty>::const_iterator beginIter = inforIn.begin();
list<_Ty>::const_iterator endIter = inforIn.end();
list<_Ty>::size_type i = 0;
T abj;
CPtrMaker<T, _Ty>::FuncPtr funcPtr = FuncPtr;
for(listIter = beginIter; listIter != endIter; ++i, ++listIter)
{
if(inforIn.size() <= PER_REPLY_LEN)
{
listCopy.resize(inforIn.size());
copy(beginIter, endIter, listCopy.begin());
std::cout << "容器分段,长度小于" << PER_REPLY_LEN << std::endl;
(abj.*funcPtr)(listCopy); //函数指针的使用方式
break;
}
else if(i && !(i%PER_REPLY_LEN))
{
if((i+5) >= inforIn.size())
{
listCopy.resize(PER_REPLY_LEN);
copy(beginIter, listIter, listCopy.begin());
std::cout << "容器分段, 第" << i/PER_REPLY_LEN << "包" << std::endl;
(abj.*funcPtr)(listCopy);
listCopy.resize(inforIn.size()-i);
copy(listIter, endIter, listCopy.begin());
std::cout << "容器分段,最后一包" << std::endl;
(abj.*funcPtr)(listCopy);
break;
}else
{
listCopy.resize(PER_REPLY_LEN);
copy(beginIter, listIter, listCopy.begin());
std::cout << "容器分段, 第" << i/PER_REPLY_LEN << "包" << std::endl;
(abj.*funcPtr)(listCopy);
}
beginIter = listIter;
}
}
}
class CReplyInt
{
public:
/*输出容器中的整数*/
void show(const list<int>& listCopy)
{
list<int>::const_iterator Iter;
for(Iter = listCopy.begin(); Iter != listCopy.end(); ++Iter)
{
std::cout << *Iter << std::endl;
}
}
};
class CReplyString
{
public:
/*输出容器中的字符串*/
void show(const list<std::string>& listCopy)
{
list<std::string>::const_iterator Iter;
for(Iter = listCopy.begin(); Iter != listCopy.end(); ++Iter)
{
std::cout << (*Iter).c_str() << std::endl;
}
}
};
//注意调用类中非静态成员函数的时候,使用的是类名::函数名;而不是实例名::函数名。
class A
{
public:
int m_int;
void (*m_ptr)();
static void staticmember(){cout<<"static"<<endl;} //static member
void nonstatic(){cout<<"nonstatic"<<endl;} //nonstatic member
void nonstatic1(){cout<<"nonstatic"<<endl;} //nonstatic member
virtual void virtualmember(){cout<<"virtual"<<endl;};//virtual member
};
int main()
{
list<int> listInt;
for(int i =0; i < 4; i++)
listInt.push_back(i);
//定义CReplyInt类的成员函数指针
CPtrMaker<CReplyInt, int>::FuncPtr funcInt = &CReplyInt::show;
ReplyInfor(listInt, funcInt);
list<std::string> listString;
listString.push_back("zero");
listString.push_back("one");
listString.push_back("two");
listString.push_back("three");
listString.push_back("four");
listString.push_back("five");
listString.push_back("six");
listString.push_back("seven");
listString.push_back("eight");
listString.push_back("nine");
listString.push_back("ten");
listString.push_back("eleven");
listString.push_back("twelve");
listString.push_back("thirteen");
listString.push_back("fourteen");
listString.push_back("fifteen");
listString.push_back("sixteen");
listString.push_back("seventeen");
listString.push_back("eighteen");
listString.push_back("nineteen");
listString.push_back("twenty");
listString.push_back("twenty one");
listString.push_back("twenty two");
//定义CReplyString类的成员函数指针
CPtrMaker<CReplyString, std::string>::FuncPtr funcString = &CReplyString::show;
ReplyInfor(listString, funcString);
return 0;
}