Search code examples
c++windowsvisual-studio-2010boostheap-memory

How to create std::string wrapper that would keep ptr to std::string and ptr to special class function from class that created that wrapper instance?


I have a class. It has a function void delete_ptr(void *); and void push_into(std::set<my_string> ** pps);. I have 10 instances of this class. Each one is allocated on difrent heap. I have my programm in the main heap, same where my map is. I want to call each of my class instances push_into with my map from main. each of push_into will call (**pps).insert(std::string("Hello str")); or (**pps).insert("Hello char");. How to create such my_string class that would keep **std::string in it and also ptr to delete_ptr of class that creates my_string class instance?

Main objectives I wonder about are:

  • how to create its constructor to make it keep pointers to string and delete_ptr of instance that created it?

Solution

  • You can use boost::shared_ptr with custom deallocator. Provide your delete_ptr as a custom deallocator using boost::bind