隱式共享是QT設計的一個重要特點。
QT隱式共享的原文是這樣的(來源於https://doc.qt.io/archives/qt-4.8/implicit-sharing.html):
“Many C++ classes in Qt use implicit data sharing to maximize resource usage and minimize copying. Implicitly shared classes are both safe and efficient when passed as arguments, because only a pointer to the data is passed around, and the data is copied only if and when a function writes to it, i.e., copy-on-write.”
“When dealing with shared objects, there are two ways of copying an object. We usually speak about deep and shallow copies. A deep copy implies duplicating an object. A shallow copy is a reference copy, i.e. just a pointer to a shared data block. Making a deep copy can be expensive in terms of memory and CPU. Making a shallow copy is very fast, because it only involves setting a pointer and incrementing the reference count."
"Object assignment (with operator=()) for implicitly shared objects is implemented using shallow copies.”
“When implementing your own implicitly shared classes, use the QSharedData and QSharedDataPointer classes.”
總結幾句話就是:
1)QT裏對很多C++類採用了隱式共享的方法;隱式共享保證了高效共享數據;
2)隱式共享實質就是淺拷貝;QT認爲深拷貝過於耗費內存和CPU;
3)如果用隱式共享,用QSharedData and QSharedDataPointer 這兩種類。
由上可以得知,所謂隱式共享,不過是淺拷貝;同時,保證了淺拷貝有引用計數,避免刪除。
QSharedData和QSharedDataPointer是可以用於隱式共享的QT方法。
目前,QT裏默認隱式共享的方法是:
QGLColormap class is used for installing custom colormaps into a QGLWidget |
|
Array of bits |
|
Monochrome (1-bit depth) pixmaps |
|
Defines the fill pattern of shapes drawn by QPainter |
|
Array of bytes |
|
Template class that provides a cache |
|
Template class that provides a contiguous cache |
|
Mouse cursor with an arbitrary shape |
|
Access to directory structures and their contents |
|
System-independent file information |
|
Specifies a font used for drawing text |
|
General information about fonts |
|
Font metrics information |
|
Font metrics information |
|
Used in combination with QBrush to specify gradient fills |
|
Template class that provides a hash-table-based dictionary |
|
Scalable icons in different modes and states |
|
Hardware-independent image representation that allows direct access to the pixel data, and can be used as a paint device |
|
Encapsulates a key sequence as used by shortcuts |
|
Template class that provides linked lists |
|
Template class that provides lists |
|
Converts between numbers and their string representations in various languages |
|
Template class that provides a skip-list-based dictionary |
|
Convenience QHash subclass that provides multi-valued hashes |
|
Convenience QMap subclass that provides multi-valued maps |
|
Container for painting operations, enabling graphical shapes to be constructed and reused |
|
Contains color groups for each widget state |
|
Defines how a QPainter should draw lines and outlines of shapes |
|
Paint device that records and replays QPainter commands |
|
Off-screen image representation that can be used as a paint device |
|
Vector of points using integer precision |
|
Vector of points using floating point precision |
|
Generic container that provides a queue |
|
Pattern matching using regular expressions |
|
Specifies a clip region for a painter |
|
Template class that provides a hash-table-based set |
|
Manipulates the fields in SQL database tables and views |
|
Means of executing and manipulating SQL statements |
|
Encapsulates a database record |
|
Template class that provides a stack |
|
Unicode character string |
|
List of strings |
|
Way of finding Unicode text boundaries in a string |
|
Offers an API to access and modify QTextDocuments |
|
Represents a piece of formatted text from a QTextDocument |
|
Formatting information for a QTextDocument |
|
Convenient interface for working with URLs |
|
Acts like a union for the most common Qt data types |
|
Template class that provides a dynamic array |
|
Information about the X display configuration |