Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
9
class
MyClass
{};
10
class
MySubClass
:
public
MyClass
{};
11
12
QIODevice
*
handsOverOwnership
();
13
void
process
(QIODevice *device);
14
int
m_value
;
15
16
//! [0]
17
void
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
42
namespace
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
65
void
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
102
namespace
class_repetition
103
{
104
//! [4]
105
class
MyPrivateClass
;
// forward declare MyPrivateClass
106
107
class
MyClass
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
123
class
MyPrivateClass
{};
124
}
125
126
class
MyCustomClass
{};
127
void
myCustomDeallocator
(
MyCustomClass
*pointer) {}
128
129
//! [5]
130
// this QScopedPointer deletes its data using the delete[] operator:
131
QScopedPointer
<
int
,
QScopedPointerArrayDeleter
<
int
> >
arrayPointer
(
new
int
[42]);
132
133
// this QScopedPointer frees its data using free():
134
QScopedPointer
<
int
,
QScopedPointerPodDeleter
>
podPointer
(
reinterpret_cast
<
int
*>(malloc(42)));
135
136
// this struct calls "myCustomDeallocator" to delete the pointer
137
struct
ScopedPointerCustomDeleter
138
{
139
static
inline
void
cleanup
(
MyCustomClass
*pointer)
140
{
141
myCustomDeallocator
(
pointer
)
;
142
}
143
};
144
145
// QScopedPointer using a custom deleter:
146
QScopedPointer
<
MyCustomClass
,
ScopedPointerCustomDeleter
>
customPointer
(
new
MyCustomClass
);
147
//! [5]
MyClass
[5]
Definition
doc_src_properties.cpp:68
MyClass::MyClass
MyClass()
[5]
Definition
doc_src_resources.cpp:25
MyCustomClass
Definition
src_corelib_tools_qscopedpointer.cpp:126
MySubClass
Definition
src_corelib_tools_qscopedpointer.cpp:10
class_repetition::MyClass
Definition
src_corelib_tools_qscopedpointer.cpp:108
class_repetition::MyClass::MyClass
MyClass()
class_repetition::MyClass::~MyClass
~MyClass()
Definition
src_corelib_tools_qscopedpointer.cpp:114
class_repetition::MyPrivateClass
[4]
Definition
src_corelib_tools_qscopedpointer.cpp:123
repetition::MyClass
Definition
src_corelib_tools_qscopedpointer.cpp:44
repetition::MySubClass
Definition
src_corelib_tools_qscopedpointer.cpp:45
class_repetition
Definition
src_corelib_tools_qscopedpointer.cpp:103
repetition
[3]
Definition
src_corelib_global_qglobal_widgets.cpp:93
repetition::myFunction
void myFunction(bool useSubClass)
[1]
Definition
src_corelib_tools_qscopedpointer.cpp:48
__has_include
#define __has_include(x)
Definition
qcompilerdetection.h:459
arrayPointer
QScopedPointer< int, QScopedPointerArrayDeleter< int > > arrayPointer(new int[42])
[5]
myCustomDeallocator
void myCustomDeallocator(MyCustomClass *pointer)
Definition
src_corelib_tools_qscopedpointer.cpp:127
handsOverOwnership
QIODevice * handsOverOwnership()
podPointer
QScopedPointer< int, QScopedPointerPodDeleter > podPointer(reinterpret_cast< int * >(malloc(42)))
m_value
int m_value
Definition
src_corelib_tools_qscopedpointer.cpp:14
myFunction
void myFunction(bool useSubClass)
[0]
Definition
src_corelib_tools_qscopedpointer.cpp:17
customPointer
QScopedPointer< MyCustomClass, ScopedPointerCustomDeleter > customPointer(new MyCustomClass)
process
void process(QIODevice *device)
ScopedPointerCustomDeleter
Definition
src_corelib_tools_qscopedpointer.cpp:138
ScopedPointerCustomDeleter::cleanup
static void cleanup(MyCustomClass *pointer)
Definition
src_corelib_tools_qscopedpointer.cpp:139
qtbase
src
corelib
doc
snippets
code
src_corelib_tools_qscopedpointer.cpp
Generated on
for Qt by
1.18.0