QSharedData and operator =

I recently wanted to implement implicit sharing functions like Qt with its QSharedData and QSharedDataPointer , so I looked at their sources and instead of QSharedData found these three lines:

 private: // using the assignment operator would lead to corruption in the ref-counting QSharedData &operator=(const QSharedData &); 

However, I do not understand how operator= to break the reference count.

If I simply did not make it closed and left its implementation empty, would it serve the same purpose?

i.e. if I just wrote this:

  public: QSharedData &operator=(const QSharedData & ) { return *this; } 
+4
source share
2 answers

The purpose of QSharedData is to maintain reference counting. If you assign each other, what should happen with the counting of links on each side? As you correctly identified: nothing. It just doesn’t make sense to assign one QSharedData another, and therefore a reasonable course of action is to prevent it at compile time.

+4
source

No, it would be bad if he did the reference counting, he needed to keep accounting records, and just returning this means that there are copies of QSharedData unaccounted for. this C ++ faq example basically shows what accounting is necessary for operator = in a counted object.

+2
source

Source: https://habr.com/ru/post/1468921/


All Articles