boost::shared_ptr pimpl and Qt

Posted on 2009-04-22
Medium Priority
Last Modified: 2012-05-06
I recently adopted the pimpl idiom in my code as a clean way of ensuring appropriate behavior and destruction and encapsulation of the shared_ptrs I was passing around previously. I've been implementing it as a boost::shared_ptr to a forward-declared "-Impl" class defined as a struct in the .cpp file.

What I've found, however, is that Qt doesn't like the idea of shared state across objects much at all. I have found a consistent need to create QLists and QMaps of objects that extend QObject, and I had become tired of the extra typing required in making everything a list of MyObject* or shared_ptr<MyObject>, so I had hoped pimpl would resolve this difficulty. As it turns out, connected Qt signals only work on the original object, not its copy, so I had to implement assignment operators and copy constructors to ensure the signals connected in the constructor stayed connected. This has the unfortunate side effect of allowing an object to be copied AFTER signals are assigned externally, and the object has no way of knowing dynamically what it needs to reassign.

It gets worse when multiple threads are involved. If an object creates a background thread for itself during construction, it passes a boost binding to "this" for the thread to call, then after construction it gets copied and destructed, and the "this" reference the thread has is no longer valid. I got around this by passing the pimpl to the thread instead, and modifying the "parent" object of the pimpl during copy and assignment.

Now my code at least works, but in all reality it's no more than an auto_ptr in shared_ptr's clothing, with none of the type safety.

Is there an obvious solution to this? Can Qt support shared pimpls, or is it a lost cause? Do I need to go back to shared_ptrs if I need shared objects?
Question by:dberner9
1 Comment
LVL 30

Accepted Solution

Axter earned 1500 total points
ID: 24220936
Consider using a clone_ptr or smart_ptr.
The following link has a smart_ptr which can be use to create a clone_ptr.

Clone pointers have logic which allows object to clone itself, even if it's a derived class pointing to a base class smart pointer.

Check out following link for more info:


Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question