Answer the question
In order to leave comments, you need to log in
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);
#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
There is such a rule: "const by default", i.e. const should be used wherever possible. In this case, it QString
is passed by reference and is scheduled to be read-only, so it is made const.
In addition, for copied objects const
it 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 QString
it 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 const
for the reference argument.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question