summaryrefslogtreecommitdiffstats
path: root/src/corelib/doc/snippets/sharedemployee/employee.h
blob: 28f68887caf8e26e25fb34d9556f29dbe3573b84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#ifndef EMPLOYEE_H
#define EMPLOYEE_H

//! [0]
#include <QSharedData>
#include <QSharedDataPointer>
#include <QString>

class EmployeeData : public QSharedData
{
public:
    EmployeeData() : id(-1) {}
    EmployeeData(const EmployeeData &other)
        : QSharedData(other), id(other.id), name(other.name) {}
    ~EmployeeData() = default;

    int id;
    QString name;
};

class Employee
{
public:
    //! [1]
    Employee() { d = new EmployeeData; }
    //! [1] //! [2]
    Employee(int id, const QString &name) {
        d = new EmployeeData;
        setId(id);
        setName(name);
    }
    //! [2] //! [7]
    Employee(const Employee &other) = default;
    Employee &operator=(const Employee &other) = default;

    Employee(Employee &&other) = default;
    Employee &operator=(Employee &&other) = default;

    ~Employee() = default;

    //! [7]
    //! [3]
    void setId(int id) { d->id = id; }
    //! [3] //! [4]
    void setName(const QString &name) { d->name = name; }
    //! [4]

    //! [5]
    int id() const { return d->id; }
    //! [5] //! [6]
    QString name() const { return d->name; }
    //! [6]

private:
    QSharedDataPointer<EmployeeData> d;
};
//! [0]

#endif