这是否与模板在编译时动态绑定有关???
#include <iostream>
#include <string>
#include <boost/function.hpp>
#include <vector>
class CBase {
public:
//CBase();
//~CBase();
public:
virtual void print(const std::string& vInfo) {std::cout << "base class member print function:" << vInfo << std::endl;}
};
class CDerive : public CBase {
public:
//CDerive();
//~CDerive(); //why?
public:
virtual void print(const std::string& vInfo) {std::cout << "derive class member print function:" << vInfo << std::endl;}
};
void globalPrint(const std::string& vInfo)
{
std::cout << "global print function:" << vInfo << std::endl;
}
class FunctionObj {
public:
//FunctionObj();
//~FunctionObj(); why?
public:
void operator()(const std::string& vInfo) {std::cout << "function object print function:" << vInfo << std::endl;}
};
int main (int argc, char * const argv[]) {
// insert code here...
std::vector<boost::function<void(const std::string&)> > FuncVec;
FuncVec.push_back(&globalPrint);
FuncVec.push_back(FunctionObj());
FuncVec[0]("0");
FuncVec[1]("1");
boost::function<void(CBase*, const std::string&)> Func0;
Func0 = &CBase::print;
CBase base;
Func0(&base, "2");
boost::function<void(CDerive*, const std::string&)> Func1; //why can't be CBase
CDerive derive;
Func1 = &CDerive::print;
Func1((&derive), "3");
return 0;
}