[Qt-interest] Const correctness with QSharedPointer

Thiago Macieira thiago at kde.org
Tue Dec 8 09:13:51 CET 2009


Em Segunda-feira 07 Dezembro 2009, às 23:26:56, Colin Kern escreveu:
> On Mon, Dec 7, 2009 at 4:31 PM, Stephen Jackson <spjackson42 at gmail.com> 
wrote:
> > 2009/12/7 Brad Howes :
> >> On Dec 7, 2009, at 3:31 PM, Colin Kern wrote:
> >>> I want to pass a QSharedPointer to a function as a const pointer, so
> >>> that the compiler will enforce that I don't mutate the object the
> >>> shared pointer is pointing to.  Can I do this with "void func(const
> >>> QSharedPointer<A> p);" or does that just enforce that I won't change
> >>> what the shared pointer is pointing to?
> 
> I'm going to hijack my own thread here, since I have another question
> about QSharedPointers.  Is there a way to maintain polymorphism when
> using QSharedPointers?  This code works:
> 
> class A { };
> class B : public A { };
> void func(A* p) { };
> int main()
> {
>     B *p = new B();
>     func(p);
> }
> 
> but this doesn't compile:
> 
> #include <QSharedPointer>
> class A { };
> class B : public A { };
> void func(QSharedPointer<A> p) { };
> int main()
> {
>     QSharedPointer<B> p(new B());
>     func(p);
> }

Cannot reproduce.

$ cat > main.cpp
#include <QSharedPointer>
class A { };
class B : public A { };
void func(QSharedPointer<A> p) { };
int main()
{
    QSharedPointer<B> p(new B());
    func(p);
}
$ qmake -project QT-=gui
$ qmake
$ make
[  0%] Compiling main.cpp
main.cpp:4: warning: unused parameter 'p'
[ 50%] Linking executable qsp
$ valgrind ./qsp
==24757== Memcheck, a memory error detector
==24757== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==24757== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info
==24757== Command: ./qsp
==24757==
==24757==
==24757== HEAP SUMMARY:
==24757==     in use at exit: 0 bytes in 0 blocks
==24757==   total heap usage: 11 allocs, 11 frees, 369 bytes allocated
==24757==
==24757== All heap blocks were freed -- no leaks are possible
==24757==
==24757== For counts of detected and suppressed errors, rerun with: -v
==24757== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 33 from 6)

I've just added the condition to the automatic unit test for QSharedPointer in 
Qt. This will ensure that all compilers support the thing correctly.

You can see it in 
http://qt.gitorious.org/qt/qt/commit/dfbdb86fd4614be9be17cbfb602ed5a3b5a38671 
(when that becomes visible in a day or two).

diff --git a/tests/auto/qsharedpointer/tst_qsharedpointer.cpp 
b/tests/auto/qsharedpointer/tst_qsharedpointer.cpp
index ed9206c..0cb08f9 100644
--- a/tests/auto/qsharedpointer/tst_qsharedpointer.cpp
+++ b/tests/auto/qsharedpointer/tst_qsharedpointer.cpp
@@ -73,6 +73,7 @@ private slots:
     void forwardDeclaration2();
     void memoryManagement();
     void downCast();
+    void functionCallDownCast();
     void upCast();
     void qobjectWeakManagement();
     void noSharedPointerFromWeakQObject();
@@ -503,6 +504,15 @@ void tst_QSharedPointer::downCast()
     QCOMPARE(DerivedData::derivedDestructorCounter, destructorCount + 1);
 }

+void functionDataByValue(QSharedPointer<Data> p) { Q_UNUSED(p); };
+void functionDataByRef(const QSharedPointer<Data> &p) { Q_UNUSED(p); };
+void tst_QSharedPointer::functionCallDownCast()
+{
+    QSharedPointer<DerivedData> p(new DerivedData());
+    functionDataByValue(p);
+    functionDataByRef(p);
+}
+
 void tst_QSharedPointer::upCast()
 {
     QSharedPointer<Data> baseptr = QSharedPointer<Data>(new DerivedData);


-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Senior Product Manager - Nokia, Qt Development Frameworks
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091208/197861f6/attachment.bin 


More information about the Qt-interest-old mailing list