我需要一个函数,它可以做一些oparations:
typedef const bool (myClass::*DecoderFactory) (void);
DecoderFactory * decoderFactory = new DecoderFactory[1];
QVector<DecoderFactory> * decoderFunctions = new QVector<DecoderFactory>;
decoderFactory[0] = &MyClass::do0;
decoderFunctions->push_back(&MyClass::do0);
decoderFunctions->push_back(&MyClass::do1);
...
const bool someFunction(const short int code)
{
...
decoderFactory[0] = decoderFunctions->value(code);
if (!(this->*decoderFactory[0])()) return false;
return true;
}
const bool do0()
{
//do some
}
const bool do1()
{
//do some
}
...
这是我目前的方式,当然不是最好的。我可以让这更容易吗?具体来说,如何在没有
的情况下完成DecoderFactory * decoderFactory = new DecoderFactory[1];
?我怎么能用一个元素的数组来做,这很明显像sh * tcode? p>
答案 0 :(得分:1)
如果使用C ++容器和C ++函数包装器,可以更容易:
struct MyClass
{
typedef std::function<bool()> DecoderFactory;
std::vector<DecoderFactory> decoderFunctions;
MyClass() {
decoderFunctions.push_back(std::bind(&MyClass::do0, this));
decoderFunctions.push_back(std::bind(&MyClass::do1, this));
//...
}
const bool someFunction(const short int code)
{
return decoderFunctions[code /*Did you mean CODE here instead of 0?;)*/]();
}
const bool do0()
{
//do some
}
const bool do1()
{
//do some
}
};