Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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 \macro Q_DISABLE_COPY(Class)
6 \relates <QtClassHelperMacros>
7
8 Disables the use of copy constructors and assignment operators
9 for the given \a Class.
10
11 Instances of subclasses of QObject should not be thought of as
12 values that can be copied or assigned, but as unique identities.
13 This means that when you create your own subclass of QObject
14 (director or indirect), you should \e not give it a copy constructor
15 or an assignment operator. However, it may not enough to simply
16 omit them from your class, because, if you mistakenly write some code
17 that requires a copy constructor or an assignment operator (it's easy
18 to do), your compiler will thoughtfully create it for you. You must
19 do more.
20
21 The curious user will have seen that the Qt classes derived
22 from QObject typically include this macro in a private section:
23
24 \snippet code/src_corelib_global_qglobal.cpp 43
25
26 It declares a copy constructor and an assignment operator in the
27 private section, so that if you use them by mistake, the compiler
28 will report an error.
29
30 \snippet code/src_corelib_global_qglobal.cpp 44
31
32 But even this might not catch absolutely every case. You might be
33 tempted to do something like this:
34
35 \snippet code/src_corelib_global_qglobal.cpp 45
36
37 First of all, don't do that. Most compilers will generate code that
38 uses the copy constructor, so the privacy violation error will be
39 reported, but your C++ compiler is not required to generate code for
40 this statement in a specific way. It could generate code using
41 \e{neither} the copy constructor \e{nor} the assignment operator we
42 made private. In that case, no error would be reported, but your
43 application would probably crash when you called a member function
44 of \c{w}.
45
46 \sa Q_DISABLE_COPY_MOVE
47*/
48
49/*!
50 \macro Q_DISABLE_COPY_MOVE(Class)
51 \relates <QtClassHelperMacros>
52
53 A convenience macro that disables the use of copy constructors, assignment
54 operators, move constructors and move assignment operators for the given
55 \a Class.
56
57 \sa Q_DISABLE_COPY
58 \since 5.13
59*/