默认情况下,函数指针是否为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)
    // ...