Qt框架浅析之一 ------- 隐式共享(Implicit Sharing)

https://blog.csdn.net/Moke_8453/article/details/54172310

嗯,也许很多人都讲过这个Qt架构这个话题,但是我还是要讲一下这个,希望能有一点新意吧。

    我们先来看看Qt官方关于Qt隐式共享的解释:

    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.

    翻译:很多Qt中的C++类运用了隐式数据共享机制去最大化资源有效利用和最小化复制克隆操作。隐式共享类当作为函数参数传递的时候,不仅安全而且效率很高,因为传递的时候只是传递的数据的指针,数据本身只当自己被修改的时候才会去复制。简称写时复制。

    文章最后附上所有隐式共享类名单,讲一下这个东西的工作机理。比如我们往往会在信号槽中做这样的事情:

connect(yourLineEdit,SIGNAL(textEdited(QString)),this,SLOT(sltDoTextEdited(QString)));

这个时候,QString这个类作为信号槽函数的参数传递,传递的时候只会传递对应QString对象所在的地址指针,而不会去调用QString的拷贝复制函数。在C++中,为了让参数传递的时候不去调用参数的拷贝复制函数一般是加const T &,你们如果去寻找对应的信号声明也可以发现,很多信号声明的时候会加上const T &,避免拷贝函数构造和参数修改。

    这里说C++的Const T&只是为了帮助大家理解这个隐式共享的概念,并不是说Qt就是用const T &实现的隐式共享,请谨记。

    Qt5和隐式共享机制实现相关的类:QSaredData和QSharedPointer。

    qt的隐式共享类list:
QBitArray
QBitmap
QBrush
QByteArray
QByteArrayList
QCache
QCollator
QCollatorSortKey
QCommandLineOption
QContiguousCache
QCursor
QDBusPendingCall
QDBusUnixFileDescriptor
QDateTime
QDebug
QDir
QDnsDomainNameRecord
QDnsHostAddressRecord
QDnsMailExchangeRecord
QDnsServiceRecordQDnsTextRecord
QFileInfo
QFont
QFontInfo
QFontMetrics
QFontMetricsF
QGlyphRun
QGradient
QHash
QHttpPart
QIcon
QImage
QKeySequence
QLinkedList
QList
QLocale
QMap
QMimeType
QMultiHash
QMultiMap
QNetworkAddressEntry
QNetworkCacheMetaData
QNetworkConfiguration
QNetworkCookie
QNetworkInterface
QNetworkProxy
QNetworkProxyQuery
QNetworkRequest
QOpenGLDebugMessage
QPainterPath
QPalette
QPen
QPersistentModelIndex
QPicture
QPixmap
QPolygon
QPolygonF
QProcessEnvironment
QQueue
QRawFont
QRegExp
QRegion
QRegularExpression
QRegularExpressionMatch
QRegularExpressionMatchIterator
QSet
QSslCertificate
QSslCertificateExtension
QSslCipher
QSslConfiguration
QSslError
QSslKey
QStack
QStaticText
QStorageInfo
QString
QStringList
QTextBlockFormat
QTextBoundaryFinder
QTextCharFormat
QTextCursor
QTextDocumentFragment
QTextFormat
QTextFrameFormat
QTextImageFormat
QTextListFormat
QTextTableCellFormat
QTextTableFormat
QUrl
QUrlQuery
QVariant
QVector

原文地址:https://www.cnblogs.com/Vancamel/p/11346282.html