检索对象的数据类型

时间:2017-06-19 17:14:34

标签: c++ oop arduino

由于在Arduino编译器中禁用了typeid运算符,我正在寻找一种检测对象类型的方法。

我发现这个方便code。作者声明它适用于自定义类。但是如何?

我的方法是将代码放在类的开头,我想在它中使用它:

// Generic catch-all implementation.
template <typename T_ty> struct TypeInfo { static const char * name; };
template <typename T_ty> const char * TypeInfo<T_ty>::name = "unknown";

// Handy macro to make querying stuff easier.
#define TYPE_NAME(var) TypeInfo< typeof(var) >::name

// Handy macro to make defining stuff easier.
#define MAKE_TYPE_INFO(type)  template <> const char * TypeInfo<type>::name = #type;

// Type-specific implementations.
MAKE_TYPE_INFO( numfield )

class numfield{public: numfield();};
    numfield::numfield(){
}
class plus{public: plus(const numfield * nf);};
plus::plus( const numfield * nfIN){
    if (TYPE_NAME(nfIN)=="numfield") {
        nf=nfIN;
    } else {
        Serial.print("Warning: object of ");
        Serial.print(TYPE_NAME(nfIN));
        Serial.println(" type is handed to object of type puls");
    }
};

错误:

  

numfield未在MAKE_TYPE_INFO( numfield )

中定义

0 个答案:

没有答案