开发者

redefining or adding a classes member function dynamically/ during execution

Hey i'm trying to make a very simple GUI using SFML, and i want to be able to attach a function to my Button class in the co开发者_开发知识库nstructor, as so the user can define what happens when the button is pressed.

I've worked a little before with GLUT and noticed these declerations:

glutReshapeFunc(Resize);
glutDisplayFunc(Draw);

Which apparently define what function should be called when the window is resized or displayed. I want something exactly like this for my button so upon construction you can define what function should be called. I want to be able to pass a function name just like glut, not having define a new class wich overides a virtual functin.

  • I also doubt it's possible however to pass parameters for these called functions, as you never know what or how many there would be. Am i right?

So anyway..... How do i accomplish this or something like it?? Thanks!


You can store a callback using e.g. std::function (for C++0x; boost::function is also available and has a similar interface).

#include <functional>

class Button {
public:
    template<typename T>
    explicit
    Button(T const& t): callback(t) {}

    void
    press()
    {
        callback();
    }

private:
    std::function<void()> callback;
};

// example use with a lambda
Button b([] { do_stuff(); });
b.press(); // will call do_stuff


In C++ it's better to use virtual function approach to address such kind of problems. That's more maintainable at long run.

You can choose to redesign a little bit to your code, where you can have a common handle to various subclasses. Now based on subclass chosen you can call a particular function. For example:

class Shape
{
public:
  virtual void Resize () = 0;
  virtual void Draw () = 0;
};

class Triangle : public Shape
{
public:
//  implement above to functions
};

class Square : public Shape
{
public:
//  implement above to functions
};

Now, just pass the handle of Shape* wherever you want and call the above abstract methods;

void foo(Shape *p)
{
  p->Resize();
}


(Rewrote everything), I had misread the question.

You seem to be wanting to pass plain old function pointers around to other functions. All you need to do is just pass the name of the function you want, but do so inside an if (or something like that) so the function passed is actualy what you want:

if(i am feeling lucky today){
    glutDisplayFunc(DrawMyLuckyShape);
}else{
    glutDisplayFunc(DrawAFoo);
}

The bad news is that since C is a nasty language you can't set up to pass extra parameters to your functions (ie, use closures). Therefore, you need to rely on a) the functions being passed some parameter quen being called or b) the functions looking at some global state.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜