Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

c++ - 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.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...