boost::shared_ptr<int> smartInt(new int(5));
equal = smartVariant2 == smartVariant;
boost::shared_ptr<int> a = smartVariant.value<boost::shared_ptr<int> >();
boost::shared_ptr<int> b = smartVariant2.value<boost::shared_ptr<int> >();
qDebug() << a.get() << b.get();
qDebug() << a.use_count() << b.use_count();
boost::shared_ptr<int> smartInt(new int(5));
QVariant smartVariant = QVariant::fromValue(smartInt);
QVariant smartVariant2 = QVariant::fromValue(smartInt);
equal = smartVariant2 == smartVariant;
boost::shared_ptr<int> a = smartVariant.value<boost::shared_ptr<int> >();
boost::shared_ptr<int> b = smartVariant2.value<boost::shared_ptr<int> >();
qDebug() << a.get() << b.get();
qDebug() << a.use_count() << b.use_count();
To copy to clipboard, switch view to plain text mode
QVariant has made a copy of your shared_ptr in each of smartVariant and smartVariant2. They are two different smart_ptr objects (with no operator==) so the equality test fails. They do however point to the same shared data (debug line 1). Debug line 2 shows that the shared data is more shared than you think: a reference count of 5 (from smartInt, the two variants, a, and b).
Bookmarks