user1886318
user1886318

Reputation: 241

Hide dependency in the headers

I implemented the "private implementation class" like this :

#include <boost/shared_ptr.hpp>

class MyClassImpl;

class MyClass
{
    public:
        MyClass();
        ~MyClass();
        int someFunc();

    private:
        boost::shared_ptr<MyClassImpl> * pimpl;
}

But in this case, I use a boost smart pointer. I would like to hide the boost dependency (To avoid that the users have to use boost to compile) What is the best solution ?

Thank you very much.

Upvotes: 0

Views: 273

Answers (1)

Mike Seymour
Mike Seymour

Reputation: 254431

You can't hide the dependency on the smart pointer, since that's part of the "public" class.

In C++11, use std::unique_ptr, or possibly std::shared_ptr if you want sharing semantics.

If you're stuck in the past, and need sharing, then you're pretty much stuck with using or reinventing boost::shared_ptr. If you don't need sharing, either use std::auto_ptr, or a raw pointer with a destructor to delete the object. In both cases, you should declare the copy constructor and copy-assignment operator private, to prevent accidental copying of the pointer. Alternatively, you could write your own very simple non-copyable smart pointer, similar to boost::scoped_ptr.

Upvotes: 1

Related Questions