Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
238 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...