【发布时间】:2013-06-04 12:23:42
【问题描述】:
我使用 boost::any 在不知道它们类型的情况下成功保存了变量。但是我在徘徊是否可以在不将它们实际转换回原始类型的情况下使用它们。这是一个可以更好地描述问题的示例:
struct callbackInfo
{
boost::any pointer_to_the_object;
boost::any pointer_to_the_function;
};
class someClass
{
template<class T, class P>
void add(T const func, P that)
{
callbackInfo tmp;
tmp.pointer_to_the_object =that;
tmp.pointer_to_the_function = func;
functions->addLast(tmp);
}
template<class ARG>
void trigger(SENDERTYPE sender, ARG arg)
{
STLinkedListIterator<callbackInfo>* it = functions->createIteator();
if(it != nullptr)
{
do
{
//This is the problem:
((*it->getData().pointer_to_the_object).*it->getData().pointer_to_the_function)(nullptr);
it->next();
}while(!it->EOL());
}
}
}
我想我已经知道答案是否定的,因为编译器无法检查变量的类型,但无论如何我问也许有一个天才的解决方案。
【问题讨论】:
-
答案是否定的。但是你的问题不是最好用多态来解决吗(只是一个想法,因为我对实际情况知之甚少)?