M
M
Maxim Rudnev2016-07-13 16:16:32
Qt
Maxim Rudnev, 2016-07-13 16:16:32

Why Qstring constant value?

Newbie. I'm trying to dig into someone else's code
specifically in this piece:

public:
    Get(const QString &peer, const QString &objectId, QObject *parent = 0);

also I will not understand why in a prototype of method Get constant parameters of lines are specified. Any thoughts?
#pragma once

#include <QObject>
#include <QStringList>

class QUdpSocket;
class Get : public QObject
{
    Q_OBJECT

    QString peer;
    QString objectId;

    QUdpSocket *socket;

    QStringList response;
public:
    Get(const QString &peer, const QString &objectId, QObject *parent = 0);

    void execute();

    QStringList getResponse() const;

private Q_SLOTS:
    void readPendingDatagram();
};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Fat Lorrie, 2016-07-13
@stigmt

There is such a rule: "const by default", i.e. const should be used wherever possible. In this case, it QStringis passed by reference and is scheduled to be read-only, so it is made const.
In addition, for copied objects constit is not necessary, it is enough to pass them by value (the argument will be copied and only a copy of it will be available to us). But QStringit can be quite large, and the method calls are quite frequent, so just a reference is passed. It becomes possible to change the object through this link. To disable this again, the method specifies constfor the reference argument.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question