Sign in to follow this  

Function pointer or inheritance?

This topic is 4712 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

Ok I have two classes in a lib I’m making. Window and Frame, both classes have a SetRender() method. SetRender() either passes a function, or a class derived from a base class of Render. So for speed and C++ compliancy, should I pass a function or a base class? Both classes are ment to be assigned by a script at a later date… so that’s why I wanted pass the render function or class to the Frame/Window instead of directly inheriting it like you do in Java and C++.

Share this post


Link to post
Share on other sites
Which one to pass depends on what you are trying to achieve. My guidelines would be

a) If the SetRender function requires state information that can change from call to call, then pass in a polymorphic base class. This will allow you to keep state information in the derived class, which can be used by the call to the polymorphic object.

b) If the SetRender function does not require state information that will change from call to call, pass a function pointer.

In terms of speed, they are both identical (but this depends on how the virtual functions are implemented by the compiler).

Share this post


Link to post
Share on other sites

This topic is 4712 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.

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