Phil
Phil

Reputation: 627

How do I use function pointers within this class?

I have a class: Circle. It can draw either a filled circle or an outlined circle. Based on its current setting, I want a general draw() method that calls either draw_filled() or draw_outlined().

In my class, I have the member void (*draw)(void);

I have two private functions: void draw_filled(); void draw_outlined();

Then, I have the following method:

void Circle::fill(const bool fill)
{
    m_fill = fill;

    if (fill)
        draw = draw_filled;
    else
        draw = draw_outlined;
}

I get an error on both draw assignments:

error C2440: '=' : cannot convert from 'void (__thiscall X2D::GL::Circle::* )(void)' to 'void (__cdecl *)(void)' 1> There is no context in which this conversion is possible

Normally, I don't use function pointers in classes, so this is new to me. Any help would be appreciated. Thank you.

Upvotes: 0

Views: 51

Answers (1)

wolfPack88
wolfPack88

Reputation: 4203

From the error message, it appears that draw is a generic pointer to a function, and not a pointer to a member function. The declaration of draw should be void (X2D::GL::Circle::*draw)(void), so that draw points to a member function of Circle.

Upvotes: 1

Related Questions