C++设计新思维中的判断A是否继承B~

来源:互联网 发布:python抓取股票数据 编辑:程序博客网 时间:2024/04/26 19:03
template <class T,class U>class CConversion{typedef char Small;class Big{char dumppy[2];};static Small Test(U);static Big Test(...);static T MakeT();public:enum{exist=sizeof(Test(MakeT()))==sizeof(Small)};};

#include "Conversion.h"#include <iostream>class testFather{};class testChild:public testFather{};int main(){using namespace std;cout<<CConversion<testChild,testFather>::exist<<endl;}

sizeof会直接传回大小,不用等到执行期才评估。如果接受U的那个函数被调用,我们就知道T可转换成U。这边的重点是里面的自动转换。
原创粉丝点击