我有几个类有以下方法:
class CAnswer
{
...
public:
int16_t sendCheckReplies(const char send[], uint16_t timeout, ...)
{
return 64;
}
//------------------------------------------------------------------------
bool sendCheckReply(const char *send, const char *reply, uint16_t timeout)
{
getReply(send, timeout);
return checkReply(reply, timeout);
}
//------------------------------------------------------------------------
// Send prefix, suffix, and newline. Verify FONA response matches reply parameter.
bool sendCheckReply(const char* prefix, char *suffix, const char* reply, uint16_t timeout)
{
getReply(prefix, suffix, timeout);
return checkReply(reply, timeout);
}
//------------------------------------------------------------------------
...
};
而是将每个方法修改为:
bool sendCheckReply(const char *send, const char *reply, uint16_t timeout, int count)
{
for(int i=0; i<count; i++)
{
getReply(send, timeout); // puts the reply in _replybuffer[1500];
if(true == checkReply(reply, timeout))
return true;
delay(2);
}
return false;
}
//------------------------------------------------------------------------
我想写一些像:
int main(void)
{
CAnswer answer;
bool bret = false;
bret = repeatUntil(true, answer.sendCheckReply("ATV0", "OK", 1000), 5);
std::cout << "bret: " << bret << std::endl;
return 0;
}
repeatUntil()应该有类似于:
的签名 bool repeatUntil(bool bretOk, bool (CAnswer::*pM)(), int iCount)
where:
bretOk: is the value should be returned by the method pointed by pM because repeatUntil returns true;
count: is the maximum number of trials pM can be called if doesn't return bretOk.
当然问题是指向该方法的指针的签名并不总是相同的,我会尽可能地编写一个C ++函数。
我有一个C ++ 11/14兼容的编译器。是否有类似于&#34;泛型方法指针&#34;由C ++ 11/14引入或者我需要使用模板吗?或者在这种情况下还有比模板更好的东西吗?
感谢。