Qt uses implicit sharing in some classes. So it does for QString:
Qt Code:
  1. Foo::Foo()
  2. {
  3. QAction *a = QAction(this);
  4. a->setData(QString("Hello world"));
  5. }
  6.  
  7. Foo::~Foo()
  8. {
  9. // a gets destroyed and so also the string since a takes care of that. (And it the reference count is equal 0.
  10. }
  11.  
  12. Foo::mySlot()
  13. {
  14. QString s = a->data()->toString(); // s is not a real copy (as long as you don't change it
  15. } // s gets deleted (or the reference count gets decreased.
To copy to clipboard, switch view to plain text mode