Sign in to follow this  
BBB

Function pointers in classes

Recommended Posts

I know that this question has been asked a hundred times and i once saw a answer by null_pointer that answered my question and ive searched for that thread but can't find it :/ . Anyway, here it goes: I have a interface:

class Ifoo
 {
  public:
  typedef void (Ifoo::*RenderFuncPTR)();
  RenderFuncPTR RenderPTR;
 }; // end class Ifoo




and in a derived class i want to link the function pointer to another class function:

class Foo2 : public Ifoo
 {
  protected:
  virtual void m_render() { }
  public:
  Foo2() { RenderPTR = &Foo2::m_render; }
 }; // end class Foo2 : public Ifoo




I know that i can use virtual functions and just re-create the object but that is not very flexible. If i could get function pointers in classes to work it would make things alot easier. The derived class must be able to link a inherited function pointer to one of it's member functions. Im using Dev-Cpp on Win2k and GCC on Fedora Core 2 and it would be nice if i could make the code work on PowerPC and SPARC in the future (ive seen people using __declspec with function pointers in classes and i don't know if it's x86 specific). [Edited by - BBB on September 19, 2004 11:23:16 AM]

Share this post


Link to post
Share on other sites
There is no reason to use function pointer like that in c++. You want to use interfaces here. Function pointers are not flexible they are error prone and make the code look ugly.


class Renderable
{
public:
void render() const =0;
virtual ~Renderable(){}
};

class Foo
{
public:
void virtual setRenderable(const Renderable*);
void virtual callRender(); // will call render

virtual ~Foo(){}

private:
const Renderable* stuffToRender;
};

class Foo2 : public Foo
{
public:
Foo2() {Foo:setRenderable(somethigRenderable);}
};


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this