Jump to content
  • Advertisement
Sign in to follow this  
BBB

Function pointers in classes

This topic is 5195 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!