tags:

views:

83

answers:

1

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);
};
+6  A: 

What you have isn't legal. You have to initialize it in the constructor:

AguiWidgetBase::AguiWidgetBase() :
onPaintCallback(0)
{}

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)
    // ...
GMan
Okay Thanks ... :), Im guessing theres no way around that right? I wanted to avoid having a function to initialize function pointers,
Milo
@Milo: You don't need one. GMan's code is equivalent to what you think you want...
Oli Charlesworth
Yea but I have a callback pointer for each event, and if the user handles the event it sets the function pointer
Milo
Hence why I need to check for NULL
Milo
@Milo: Not really. That's just how things work in C++; if you want it initialized, you tell the compiler how. I've added an alternative, though.
GMan
Is there a non-boost alternative?
Milo
isn't this feature is included in C++0x?
Donotalo
If you don't want to use boost and have a C++0x compiler you can use std::function<void(AguiRectangle)>.
Ferruccio
But what about users if the library without said compiler?
Milo
@Donotalo: Yeah. @Milo: Then that's too bad. You have to make a choice: Use the C++0x `function` and say "you need to support that", use Boost's and say "you need to support that", write your own, or stick with a function pointer.
GMan