感謝您之前的幫助,我就做這樣的事情:這裏是某種工作結果(簡體) 。我用它具有相當簡單的範圍功能
//Define your template function
template<typename Type>
void fnRangeValue(CMyClass * poMyObject, std::string strFormat){
Type tMyField, tMinValue, tMaxValue;
/*Do what you have to here!*/
}
//Define a macro for your pointerFunction
typedef void (*fnPointerFunctionRange)(CMyClass * poMyObject, std::string strFormat);
// Define your lookup table (map)
const std::map<std::string, fnPointerFunctionRange> ST_FORMAT_RANGE_POINTER= {
{"UINT8",&fnRangeValue<uint8_t>},
{"STR1UINT8",&fnRangeValue<uint8_t>},
{"UINT16",&fnRangeValue<uint16_t>},
{"STR2UINT16",&fnRangeValue<uint16_t>},
{"STR4UINT16",&fnRangeValue<uint16_t>},
{"UINT32",&fnRangeValue<uint32_t>},
{"INT8",&fnRangeValue<int8_t>},
{"INT16",&fnRangeValue<int16_t>},
{"STR3INT16",&fnRangeValue<int16_t>},
{"INT32",&fnRangeValue<int32_t>},
{"FLT32",&fnRangeValue<float>},
{"FLT64",&fnRangeValue<double>},
{"STR7FL64",&fnRangeValue<double>},
{"STR8FL64",&fnRangeValue<double>},
};
void fnRangeField(CMyClass * poMyObject){
std::string strFormat;
fnPointerFunctionRange poFonctionRange;
strFormat = "UINT8";
auto itMapRangePointer = ST_EOIIM_FORMAT_RANGE_POINTER.find(strFormat);
if(itMapRangePointer != ST_FORMAT_RANGE_POINTER.end()){
poFonctionRange = ST_FORMAT_RANGE_POINTER.at(strFormat);
// Call of the right template function thanks to pointers
poFonctionRange(poMyObject,strFormat);
}
}
希望它能幫助你!
謝謝,就是這樣。你讓我今天一整天都感覺很好!我會盡量記住,當處理指向子類的指針時。 – MikiLoz