Quote Originally Posted by mattc View Post
AFAIK, binding a temporary to a const reference is perfectly legal in C++, I am doing that all the time.

I guess my problem has to do with the infamous "implicit sharing" of Qt data types. I think in this case it is breaking the C++ rules in a very dangerous way.
But did you checked that using non-Qt non-implicit shared class will give you result you expect?
I didn't know so I checked (it is not that hard) and I think that you're wrong.

Check this code:
Qt Code:
  1. #include <iostream>
  2.  
  3. class List
  4. {
  5. public:
  6. List() : head(0) {}
  7. ~List();
  8. char operator[](int index) const;
  9. List &prepend(char item);
  10.  
  11. private:
  12. struct Elem {
  13. char data;
  14. Elem *next;
  15. };
  16. Elem *head;
  17. };
  18.  
  19. List::~List()
  20. {
  21. for (Elem *e = head; e != 0;) {
  22. Elem *d = e;
  23. e = e->next;
  24. delete d;
  25. }
  26. }
  27.  
  28. char List::operator[](int index) const
  29. {
  30. Elem *tmp = head;
  31. for (int i = 0; i < index; ++i) {
  32. tmp = tmp->next;
  33. }
  34. return tmp->data;
  35. }
  36.  
  37. List &List::prepend(char item)
  38. {
  39. Elem *e = new Elem;
  40. e->data = item;
  41. e->next = head;
  42. head = e;
  43. return *this;
  44. }
  45.  
  46. int main()
  47. {
  48. const List &l = List().prepend('a');
  49. std::cout << l[0] << std::endl;
  50.  
  51. List s;
  52. const List &t = s.prepend('b');
  53. std::cout << t[0] << std::endl;
  54.  
  55. return 0;
  56. }
To copy to clipboard, switch view to plain text mode 
There is no Qt class nor implicit sharing. It is simple list implementation, with prepend() method (the same idea as in append, but in my case prepend was easier to implement). What output you expect?