默认情况下,函数指针是否为NULL? [英] Will a function pointer be NULL by default?
本文介绍了默认情况下,函数指针是否为NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面的类中,这是否意味着onPaintCallback是NULL,或者必须在类构造函数中使其为NULL?我想在开始检查NULL之前给它一个有效的指针。
In the class below, would this mean that onPaintCallback is NULL, or must I make it NULL in the class constructor? I want to start checking for NULL before it is given a valid pointer.
class AguiWidgetBase
{
virtual void onPaint();
void (*onPaintCallback)(AguiRectangle clientRect) = 0;
public:
AguiWidgetBase(void);
~AguiWidgetBase(void);
};
推荐答案
您必须在构造函数中初始化它:
What you have isn't legal. You have to initialize it in the constructor:
AguiWidgetBase::AguiWidgetBase() :
onPaintCallback(0)
{}
您可以使用 boost :: function< void(AguiRectangle )>
,除了更灵活外,还可以将其自身初始化为null。您可以查看:
You could use boost::function<void(AguiRectangle)>
, which aside from being more flexible, initializes itself correctly to null. You can check it like:
if (f)
// ...
这篇关于默认情况下,函数指针是否为NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文