为什么使用boost::function绑定类成员函数时,类的析构函数不能为虚函数

来源:互联网 发布:神州融大数据风控平台 编辑:程序博客网 时间:2024/05/21 08:45

这是否与模板在编译时动态绑定有关???

#include<iostream>

#include<string>

#include<boost/function.hpp>

#include<vector>


class CBase {

public:

//CBase();

//~CBase();

public:

virtualvoid print(const std::string& vInfo) {std::cout <<"base class member print function:" << vInfo << std::endl;}

};


class CDerive :public CBase {

public:

//CDerive();

//~CDerive(); //why?


public:

virtualvoid 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:

voidoperator()(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;

CBasebase;

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;

}



原创粉丝点击