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
qtclasshelpermacros.qdoc
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5 \headerfile <QtClassHelperMacros>
6 \inmodule QtCore
7 \ingroup funclists
8 \brief Class helper macros.
9*/
10
11/*!
12 \macro Q_DISABLE_COPY(Class)
13 \relates <QtClassHelperMacros>
14
15 Disables the use of copy constructors and assignment operators
16 for the given \a Class.
17
18 Instances of subclasses of QObject should not be thought of as
19 values that can be copied or assigned, but as unique identities.
20 This means that when you create your own subclass of QObject
21 (director or indirect), you should \e not give it a copy constructor
22 or an assignment operator. However, it may not enough to simply
23 omit them from your class, because, if you mistakenly write some code
24 that requires a copy constructor or an assignment operator (it's easy
25 to do), your compiler will thoughtfully create it for you. You must
26 do more.
27
28 The curious user will have seen that the Qt classes derived
29 from QObject typically include this macro in a private section:
30
31 \snippet code/src_corelib_global_qglobal.cpp 43
32
33 It declares a copy constructor and an assignment operator in the
34 private section, so that if you use them by mistake, the compiler
35 will report an error.
36
37 \snippet code/src_corelib_global_qglobal.cpp 44
38
39 But even this might not catch absolutely every case. You might be
40 tempted to do something like this:
41
42 \snippet code/src_corelib_global_qglobal_widgets.cpp 5
43
44 First of all, don't do that. Most compilers will generate code that
45 uses the copy constructor, so the privacy violation error will be
46 reported, but your C++ compiler is not required to generate code for
47 this statement in a specific way. It could generate code using
48 \e{neither} the copy constructor \e{nor} the assignment operator we
49 made private. In that case, no error would be reported, but your
50 application would probably crash when you called a member function
51 of \c{w}.
52
53 \sa Q_DISABLE_COPY_MOVE, Q_DISABLE_COPY_X
54*/
55
56/*!
57 \macro Q_DISABLE_COPY_MOVE(Class)
58 \relates <QtClassHelperMacros>
59
60 A convenience macro that disables the use of copy constructors, assignment
61 operators, move constructors and move assignment operators for the given
62 \a Class.
63
64 \sa Q_DISABLE_COPY, Q_DISABLE_COPY_MOVE_X
65 \since 5.13
66*/
67
68/*!
69 \macro Q_DISABLE_COPY_X(Class, reason)
70 \relates <QtClassHelperMacros>
71 \since 6.9
72
73 Like Q_DISABLE_COPY, this macro disables copy operations for the
74 class \a Class.
75
76 In addition, this documents the \a reason why this class does not support
77 copy operations. In C++26 this will cause the compiler to report that
78 reason in its error message against any code that attempts these
79 unsupported operations.
80
81 \sa Q_DISABLE_COPY, Q_DISABLE_COPY_MOVE_X, Q_DECL_EQ_DELETE_X
82*/
83
84/*!
85 \macro Q_DISABLE_COPY_MOVE_X(Class, reason)
86 \relates <QtClassHelperMacros>
87 \since 6.9
88
89 Like Q_DISABLE_COPY_MOVE, this macro disables copy and move operations for the
90 class \a Class.
91
92 In addition, this documents the \a reason why this class does not support
93 copy/move operations. In C++26 this will cause the compiler to report that
94 reason in its error message against any code that attempts these
95 unsupported operations.
96
97 \sa Q_DISABLE_COPY_X, Q_DECL_EQ_DELETE_X
98*/