I am now hacking an old C code, try to make it more C++/Boost style:
there is a resource allocation function looks like:
my_src_type* src;
my_src_create(&src, ctx, topic, handle_src_event, NULL, NULL);
i try to wrap src by a shared_ptr:
shared_ptr<my_src_type> pSrc;
I forgot to mention just now. I need to do this as a loop
std::map<string, shared_ptr<my_src_type> > dict;
my_src_type* raw_ptr;
BOOST_FOREACH(std::string topic, all_topics)
{
my_src_create(&raw_ptr, ctx, topic, handle_src_event, NULL, NULL);
boost::shared_ptr<my_src_type> pSrc(raw_ptr);
dict[topic] = pSrc;
}
Can I do it like this?
No.
Basically, you have to do it the old C way and then convert the result to a
shared_pointersomehow.You can do it by simply initializing the shared_pointer
but beware, this will fail if the
my_src_createfunction can return an already existing object. Also, if there is amy_src_destroyfunction, it won’t be called.IMHO the cleanest way is to wrap your structure in a C++ class:
and then use shared pointer for
MySrcthe ususal way.