Solved QDataStream default in function argument
-
Hi everybody,
I need a function like
void foo(QString cmd, QDataStream s);
But the compilateur say (const QDataStream&) is private
Q_DISABLE_COPYSo i can do it with reference like
void foo(QString cmd, QDataStream &s);
And it works. But i would like the second parameter as optional like this
void foo(QString cmd, QDataStream &s = QDataStream());
But it doesn't work, i don't know the syntax.
Someone can help me ?
Thx
-
Try:
void foo(QString cmd, const QDataStream &s = QDataStream());
You are using a reference to QDataStream, therefore, you need to declare it as constant. It will make no sense to assign in the routine anyhow, since the result would be lost when returning. Not sure, if you really want to do like this, because of losing the result.
This would be a typical reason to use a pointer instead. You need to check the value of the pointer internally for zero value.
void foo(QString cmd, QDataStream *s = 0);
-
void foo(QString cmd, const QDataStream &s = QDataStream());
This way doesn't work.
i would like used reference and not pointer...
but this worksvoid foo(QString cmd, QDataStream *s = 0);
it's not possible to use references ?
-
@Franckynos said in QDataStream default in function argument:
void foo(QString cmd, const QDataStream &s = QDataStream());
This way doesn't work.
Why does it not work?
Do you have an error message or you do not like it?You have to decide whether you like to use a reference or a pointer. With both you are able to retrieve the result generated in a routine. However, if you are setting a default you got a problem with a reference, because the result will not be passed back respectively you cannot access anyhow.
-
Hi,
You already used a reference:
void foo(QString cmd, QDataStream &s);
and as @koahnig wrote, it makes no sense to try to assign a default value to the stream parameter.