Tuesday, May 14, 2013

The Amazing Shared Pointer

Quick entry tonight since I'm bogged down with work for actual work.

Did you know you could add a custom deleter to a shared pointer in boost? Try the following:

#include <boost/shared_ptr.hpp>
#include <iostream>

void doSomething(int* i)
{
    std::cout << "DOING SOMETHING" << std::endl;
}

int main(int argc, char** argv)
{
    boost::shared_ptr<int> blah(new int, &doSomething);
}

Obviously the second argument to the shared pointer can be any sort of functor (provided that operator() takes whatever the shared pointer type is).

Like I said, short and lame tonight.

No comments:

Post a Comment