Jump to content
  • Advertisement
Sign in to follow this  
KemiKal

Function Pointer Problem

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

Hi, im currently working on some container classes (just for learning purposes) and have come across a problem. I want to allow people to pass custom memory allocators to the class when they are constructed but im not sure of the best way to do this. My first thoughts were to pass a function to a constructor and then store it as a member variable but this was where problems arose. Firstly the function will need access to the protected member variables of the class, im guessing I could just pass the *this pointer to it but then the protected members would be accessible to everyone. Secondly I have a default memory alloction function in the class that is used if no allocator is supplied. The problem with this is that any function passed in wont be of the _this calling convention and therefore wont be accepted. Is there any simple ways around this problem? I guess I could just have the allocator as a virtual function and have the user inherit from it but I wanted to already inherit from my base containers to create more advanced ones. This would just leave me with several layers of inheritance making things more complicated than they should be. So does anyone have any suggestions? How does the stl take care of this problem? Thanks for any input ^_^

Share this post


Link to post
Share on other sites
Advertisement
The STL uses allocator classes that are passed to the container class as a template argument that defaults, for a container of T, to std::allocator<T>. The allocator class has allocate and deallocate functions that the container class calls for allocations and deallocations. The container class often inherits privately from the allocator class or stores a member variable of the allocator class type.

Share this post


Link to post
Share on other sites
Ah thanks for the quick reply. I guess it does make sense to remove the variables concerned with memory allocation to their own class. Ill have a deeper look at the STL I think. Thanks again, back to coding!

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!