shclass.html

来自「QT 下载资料仅供参考」· HTML 代码 · 共 231 行

HTML
231
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><!-- /home/reggie/tmp/qt-3.0-reggie-5401/qt-x11-commercial-3.0.5/doc/shclass.doc:36 --><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Shared Classes</title><style type="text/css"><!--h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }a:link { color: #004faf; text-decoration: none }a:visited { color: #672967; text-decoration: none }body { background: #ffffff; color: black; }--></style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr bgcolor="#E5E5E5"><td valign=center> <a href="index.html"><font color="#004faf">Home</font></a> | <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a> | <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a> | <a href="annotated.html"><font color="#004faf">Annotated</font></a> | <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a> | <a href="functions.html"><font color="#004faf">Functions</font></a></td><td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Shared Classes</h1><p> <!-- index reference counting --><a name="reference-counting"></a><!-- index implicit sharing --><a name="implicit-sharing"></a><!-- index explicit sharing --><a name="explicit-sharing"></a><!-- index implicitly shared --><a name="implicitly-shared"></a><!-- index explicitly shared --><a name="explicitly-shared"></a><!-- index shared implicitly --><a name="shared-implicitly"></a><!-- index shared explicitly --><a name="shared-explicitly"></a><p> Many C++ classes in Qt use <em>explicit</em> and <em>implicit</em> data sharingto maximize resource usage and minimize copying of data.<p> <!-- toc --><ul><li><a href="#1"> Overview</a><li><a href="#2"> A QByteArray Example</a><li><a href="#3"> Explicit vs. Implicit Sharing</a><li><a href="#4"> Explicitly Shared Classes</a><li><a href="#5"> Implicitly Shared Classes</a><li><a href="#6"> QCString: implicit or explicit?</a></ul><!-- endtoc --><p> <h2> Overview</h2><a name="1"></a><p> A shared class consists of a pointer to a shared data block thatcontains a reference count and the data.<p> When a shared object is created, it sets the reference count to 1.  Thereference count is incremented whenever a new object references the shareddata, and decremented when the object dereferences the shared data.  Theshared data is deleted when the reference count becomes zero.<p> <!-- index deep copy --><a name="deep-copy"></a><!-- index shallow copy --><a name="shallow-copy"></a><p> When dealing with shared objects, there are two ways of copying an object.We usually speak about <em>deep</em> and <em>shallow</em> copies.  A deep copy impliesduplicating an object.  A shallow copy is a reference copy, we only copy apointer to a shared data block.  Making a deep copy can be expensive interms of memory and CPU.  Making a shallow copy is very fast, because itonly involves setting a pointer and incrementing the reference count.<p> Object assignment (with operator=) for implicitly and explicitlyshared objects is implemented as shallow copies.  A deep copy can bemade by calling a copy() function.<p> The benefit of sharing is that a program does not need to duplicate datawhen it is not required, which results in less memory usage and lesscopying of data. Objects can easily be assigned, sent as functionarguments and returned from functions.<p> Now comes the distinction between <em>explicit</em> and <em>implicit</em> sharing.Explicit sharing means that the programmer must be aware of the fact thatobjects share common data.  Implicit sharing means that the sharingmechanism takes place behind the scenes and the programmer does not needto worry about it.<p> <h2> A <a href="qbytearray.html">QByteArray</a> Example</h2><a name="2"></a><p> QByteArray is an example of a shared class that uses explicit sharing.Example:<pre>                          //        a=         b=         c=    <a href="qbytearray.html">QByteArray</a> a(3),b(2)  // 1)     {?,?,?}    {?,?}    b[0] = 12; b[1] = 34; // 2)     {?,?,?}    {12,34}    a = b;                // 3)     {12,34}    {12,34}    a[1] = 56;            // 4)     {12,56}    {12,56}    <a href="qbytearray.html">QByteArray</a> c = a;     // 5)     {12,56}    {12,56}    {12,56}    a.<a href="qmemarray.html#detach">detach</a>();           // 6)     {12,56}    {12,56}    {12,56}    a[1] = 78;            // 7)     {12,78}    {12,56}    {12,56}    b = a.<a href="qmemarray.html#copy">copy</a>();         // 8)     {12,78}    {12,78}    {12,56}    a[1] = 90;            // 9)     {12,90}    {12,78}    {12,56}</pre> <p> The assignment <tt>a = b</tt> on line 3 throws away <tt>a</tt>'soriginal shared block (the reference count becomes zero), sets<tt>a</tt>'s shared block to point to <tt>b</tt>'s shared blockand increments the reference count.<p> On line 4, the contents of <tt>a</tt> is modified. <tt>b</tt> is also modified,because <tt>a</tt> and <tt>b</tt> refer the same data block. This is the differencebetween explicit and implicit sharing (explained below).<p> The <tt>a</tt> object detaches from the common data on line 6.  Detaching meansto copy the shared data to make sure that an object has its own privatedata.  Therefore, modifying <tt>a</tt> on line 7 will not affect <tt>b</tt> or <tt>c</tt>.<p> Finally, on line 8 we make a deep copy of <tt>a</tt> and assign it to <tt>b</tt>,so that when <tt>a</tt> is modified on line 9, <tt>b</tt> remains unchanged.<p> <h2> Explicit vs. Implicit Sharing</h2><a name="3"></a><p> Implicit sharing automatically detaches the object from a sharedblock if the object is about to change and the reference count isgreater than one. Explicit sharing leaves this job to theprogrammer.  If an explicitly shared object is not detached, changingthe object will change all other objects that refer to the same data.<p> Implicit sharing optimizes memory usage and copying of data without thisside effect.  So why didn't we implement implicit sharing for allshared classes?  The answer is that a class that allows direct access toits internal data (for efficiency reasons), like <a href="qbytearray.html">QByteArray</a>, cannot beimplicitly shared, because it can be changed without letting QByteArrayknow.<p> An implicitly shared class has total control of its internal data.  In anymember functions that modify its data, it automatically detachesbefore modifying the data.<p> The <a href="qpen.html">QPen</a> class, which uses implicit sharing, detaches from the shared datain all member functions that change the internal data.<p> Code fragment:<pre>    void QPen::setStyle( PenStyle s )    {        detach();        // detach from common data        data-&gt;style = s; // set the style member    }    void QPen::detach()    {        if ( data-&gt;count != 1 ) // only if &gt;1 reference            *this = copy();    }</pre> <p> This is clearly not possible for QByteArray, because the programmer cando the following:<p> <pre>    <a href="qbytearray.html">QByteArray</a> array( 10 );    array.<a href="qmemarray.html#fill">fill</a>( 'a' );    array[0] = 'f';        // will modify array    array.<a href="qmemarray.html#data">data</a>()[1] = 'i'; // will modify array</pre> <p> If we monitor changes in a <a href="qbytearray.html">QByteArray</a>, the QByteArray class wouldbecome unacceptably slow.<p> <h2> Explicitly Shared Classes</h2><a name="4"></a><p> All classes that are instances of the <a href="qmemarray.html">QMemArray</a> template class are explicitlyshared:<p> <ul><li> <a href="qbitarray.html">QBitArray</a><li> <a href="qpointarray.html">QPointArray</a><li> <a href="qbytearray.html">QByteArray</a><li> Any other instantiation of <a href="qmemarray.html">QMemArray&lt;type&gt;</a></ul><p> These classes have a detach() function that can be called if you want yourobject to get a private copy of the shared data.  They also have a copy()function that returns a deep copy with a reference count of 1.<p> The same is true for <a href="qimage.html">QImage</a>, which does not inherit QMemArray.  <a href="qmovie.html">QMovie</a>is also explicitly shared, but it does not support detach() and copy().<p> <h2> Implicitly Shared Classes</h2><a name="5"></a><p> The Qt classes that are implicitly shared are:<ul><li> <a href="qbitmap.html">QBitmap</a><li> <a href="qbrush.html">QBrush</a><li> <a href="qcursor.html">QCursor</a><li> <a href="qfont.html">QFont</a><li> <a href="qfontinfo.html">QFontInfo</a><li> <a href="qfontmetrics.html">QFontMetrics</a><li> <a href="qiconset.html">QIconSet</a><li> <a href="qmap.html">QMap</a><li> <a href="qpalette.html">QPalette</a><li> <a href="qpen.html">QPen</a><li> <a href="qpicture.html">QPicture</a><li> <a href="qpixmap.html">QPixmap</a><li> <a href="qregion.html">QRegion</a><li> <a href="qregexp.html">QRegExp</a><li> <a href="qstring.html">QString</a><li> <a href="qstringlist.html">QStringList</a><li> <a href="qvaluelist.html">QValueList</a><li> <a href="qvaluestack.html">QValueStack</a></ul><p> These classes automatically detach from common data if an object is aboutto be changed.  The programmer will not even notice that the objects areshared.  Thus you should treat separate instances of them as separateobjects.  They will always behave as separate objects but with the addedbonus of sharing data whenever possible.  For this reason, you can passinstances of these classes as arguments to functions by value withoutconcern for the copying overhead.<p> Example:<pre>    <a href="qpixmap.html">QPixmap</a> p1, p2;    p1.<a href="qpixmap.html#load">load</a>( "image.bmp" );    p2 = p1;                    // p1 and p2 share data    <a href="qpainter.html">QPainter</a> paint;    paint.<a href="qpainter.html#begin">begin</a>( &amp;p2 );         // cuts p2 loose from p1    paint.<a href="qpainter.html#drawText">drawText</a>( 0,50, "Hi" );    paint.<a href="qpainter.html#end">end</a>();</pre> <p> In this example, <tt>p1</tt> and <tt>p2</tt> share data until <a href="qpainter.html#begin">QPainter::begin</a>() iscalled for <tt>p2</tt>, because painting a pixmap will modify it.  The samehappens also if anything is <a href="qpaintdevice.html#bitBlt-2">bitBlt()</a>'ed into <tt>p2</tt>.<p> <h2> QCString: implicit or explicit?</h2><a name="6"></a><p> <a href="qcstring.html">QCString</a> uses a mixture of implicit and explicit sharing. Functionsinherited from <a href="qbytearray.html">QByteArray</a>, such as data(), employ explicit sharing, whilethose only in <a href="qcstring.html">QCString</a> detach automatically. Thus, QCString is rather an"experts only" class, provided mainly to ease porting from Qt 1.x to Qt 2.0.We recommend that you use <a href="qstring.html">QString</a>, a purely implicitly shared class.<p> <!-- eof --><p><address><hr><div align=center><table width=100% cellspacing=0 border=0><tr><td>Copyright &copy; 2002 <a href="http://www.trolltech.com">Trolltech</a><td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a><td align=right><div align=right>Qt version 3.0.5</div></table></div></address></body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?