Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
src_corelib_tools_qscopedpointer.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QIODevice>
5#include <QScopedPointer>
6#include <QScopedPointerArrayDeleter>
7#include <QScopedPointerPodDeleter>
8
9class MyClass {};
10class MySubClass : public MyClass {};
11
13void process(QIODevice *device);
15
16//! [0]
17void myFunction(bool useSubClass)
18{
19 MyClass *p = useSubClass ? new MyClass() : new MySubClass;
20 QIODevice *device = handsOverOwnership();
21
22 if (m_value > 3) {
23 delete p;
24 delete device;
25 return;
26 }
27
28 try {
29 process(device);
30 }
31 catch (...) {
32 delete p;
33 delete device;
34 throw;
35 }
36
37 delete p;
38 delete device;
39}
40//! [0]
41
42namespace repetition
43{
44 class MyClass {};
45 class MySubClass : public MyClass {};
46
47 //! [1]
48 void myFunction(bool useSubClass)
49 {
50 // assuming that MyClass has a virtual destructor
51 QScopedPointer<MyClass> p(useSubClass ? new MyClass() : new MySubClass);
52 QScopedPointer<QIODevice> device(handsOverOwnership());
53
54 if (m_value > 3)
55 return;
56
57 process(device.data());
58 }
59 //! [1]
60}
61
62#if __has_include(<QWidget>)
63#include <QWidget>
64
65void QWidget_snippets()
66{
67 {
68 //! [2.0]
69 const QWidget *const p = new QWidget();
70 // is equivalent to:
71 const QScopedPointer<const QWidget> p1(new QWidget());
72
73 //! [2.0]
74 }
75
76 {
77 //! [2.1]
78 QWidget *const p = new QWidget();
79 // is equivalent to:
80 const QScopedPointer<QWidget> p1(new QWidget());
81
82 //! [2.1]
83 }
84
85 {
86 //! [2.2]
87 const QWidget *p = new QWidget();
88 // is equivalent to:
89 QScopedPointer<const QWidget> p1(new QWidget());
90 //! [2.2]
91 }
92 bool scopedPointer;
93
94 //! [3]
95 if (scopedPointer) {
96 //...
97 }
98 //! [3]
99}
100#endif
101
103{
104 //! [4]
105 class MyPrivateClass; // forward declare MyPrivateClass
106
108 {
109 private:
110 QScopedPointer<MyPrivateClass> privatePtr; // QScopedPointer to forward declared class
111
112 public:
113 MyClass(); // OK
114 inline ~MyClass() {} // VIOLATION - Destructor must not be inline
115
116 private:
117 Q_DISABLE_COPY(MyClass) // OK - copy constructor and assignment operators
118 // are now disabled, so the compiler won't implicitly
119 // generate them.
120 };
121 //! [4]
122
124}
125
128
129//! [5]
130// this QScopedPointer deletes its data using the delete[] operator:
132
133// this QScopedPointer frees its data using free():
134QScopedPointer<int, QScopedPointerPodDeleter> podPointer(reinterpret_cast<int *>(malloc(42)));
135
136// this struct calls "myCustomDeallocator" to delete the pointer
138{
139 static inline void cleanup(MyCustomClass *pointer)
140 {
142 }
143};
144
145// QScopedPointer using a custom deleter:
147//! [5]
void myFunction(bool useSubClass)
[1]
#define __has_include(x)
QScopedPointer< int, QScopedPointerArrayDeleter< int > > arrayPointer(new int[42])
[5]
void myCustomDeallocator(MyCustomClass *pointer)
QIODevice * handsOverOwnership()
QScopedPointer< int, QScopedPointerPodDeleter > podPointer(reinterpret_cast< int * >(malloc(42)))
void myFunction(bool useSubClass)
[0]
QScopedPointer< MyCustomClass, ScopedPointerCustomDeleter > customPointer(new MyCustomClass)
void process(QIODevice *device)
static void cleanup(MyCustomClass *pointer)