1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
19 ** distribution.
20 ** * Neither the name of The Qt Company Ltd nor the names of its
21 ** contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 //! [0]
myFunction(bool useSubClass)42 void myFunction(bool useSubClass)
43 {
44 MyClass *p = useSubClass ? new MyClass() : new MySubClass;
45 QIODevice *device = handsOverOwnership();
46
47 if (m_value > 3) {
48 delete p;
49 delete device;
50 return;
51 }
52
53 try {
54 process(device);
55 }
56 catch (...) {
57 delete p;
58 delete device;
59 throw;
60 }
61
62 delete p;
63 delete device;
64 }
65 //! [0]
66
67 //! [1]
myFunction(bool useSubClass)68 void myFunction(bool useSubClass)
69 {
70 // assuming that MyClass has a virtual destructor
71 QScopedPointer<MyClass> p(useSubClass ? new MyClass() : new MySubClass);
72 QScopedPointer<QIODevice> device(handsOverOwnership());
73
74 if (m_value > 3)
75 return;
76
77 process(device);
78 }
79 //! [1]
80
81 //! [2]
82 const QWidget *const p = new QWidget();
83 // is equivalent to:
84 const QScopedPointer<const QWidget> p(new QWidget());
85
86 QWidget *const p = new QWidget();
87 // is equivalent to:
88 const QScopedPointer<QWidget> p(new QWidget());
89
90 const QWidget *p = new QWidget();
91 // is equivalent to:
92 QScopedPointer<const QWidget> p(new QWidget());
93 //! [2]
94
95 //! [3]
96 if (scopedPointer) {
97 ...
98 }
99 //! [3]
100
101 //! [4]
102 class MyPrivateClass; // forward declare MyPrivateClass
103
104 class MyClass
105 {
106 private:
107 QScopedPointer<MyPrivateClass> privatePtr; // QScopedPointer to forward declared class
108
109 public:
110 MyClass(); // OK
~MyClass()111 inline ~MyClass() {} // VIOLATION - Destructor must not be inline
112
113 private:
114 Q_DISABLE_COPY(MyClass) // OK - copy constructor and assignment operators
115 // are now disabled, so the compiler won't implicitely
116 // generate them.
117 };
118 //! [4]
119
120 //! [5]
121 // this QScopedPointer deletes its data using the delete[] operator:
122 QScopedPointer<int, QScopedPointerArrayDeleter<int> > arrayPointer(new int[42]);
123
124 // this QScopedPointer frees its data using free():
125 QScopedPointer<int, QScopedPointerPodDeleter> podPointer(reinterpret_cast<int *>(malloc(42)));
126
127 // this struct calls "myCustomDeallocator" to delete the pointer
128 struct ScopedPointerCustomDeleter
129 {
cleanupScopedPointerCustomDeleter130 static inline void cleanup(MyCustomClass *pointer)
131 {
132 myCustomDeallocator(pointer);
133 }
134 };
135
136 // QScopedPointer using a custom deleter:
137 QScopedPointer<MyCustomClass, ScopedPointerCustomDeleter> customPointer(new MyCustomClass);
138 //! [5]
139