[Solved] Overloaded methods can't call each other on QString &value
-
Hi, I was trying to write two overloaded functions as they are doing pretty much the same, but I need the result from the one where I am not interested in by the other:
@
bool getRawParameter(byte *data, int len);
bool getRawParameter(byte *data, int len, QString &returnValue, int retLen);
@Now as I can use the second from the first whilst just throwing away the returnValue, I thought of implementing like this:
@
bool MainWindow::getRawParameter( byte *data, int len) {
return getRawParameter( data, len, NULL, 0 );
}
@But Qt complains of not being able to convert from 0 to QString &. Using "QString()" as parameter results in not being able to convert from QString to QString&. I have tried a couble of other things, the only solution so far I found is to create a QString within this method and pass that variable without doing anything with it after the call.
I that really the only solution to it? I quite like the mechanisms of overloading and use it a lot, would you say that there is a fundamental problem in the structure I have outlined? Any other suggestions?
Thanks a lot,
Stephan -
QString& is a modifiable reference so "in-out" type. i.e you should only use it if you're going to pass something out from the function.
If that's really the case then you should do something with that result value so you can't pass a temporary QString(), as the value would be assigned to a temporary and destroyed, which would be weird. This is illegal because this is usually not intended and a bug. If it's the rare case you really mean it then yes, you need to create a separate QString variable and pass it in.If the parameter is intended to be just "in", then the type should be const QString& in which case you can use QString() to pass a default value in, as the compiler knows the temporary is not going to be assigned to.
In any case QString& is not a pointer type so you can't pass NULL there.
NULL is just a macro defined as "0", so what you do there is trying to convert integer 0 to QString, which is nonsense of course.NULL is also discouraged these days and if you mean a null pointer then there is a properly typed value for that now: nullptr. Prefer it instead of NULL or 0. nullptr is a pointer type so it will obviously not convert to QString& either.
-
Hi Chris,
thanks for the quick reply! It already helped in so far I understand that I have to "actively" throw away the result.
I was actually looking for a solution where I can decide on the parameter if the caller is interested in the result like this (I did it in Java a lot...):
@
void method( String val ) {
if ( val != null ) {
// do some processing
val = ...;
}
}
@Then I could call it like this:
@
String val;
method( val );
@Or, in order to only benefit from the internal processing, like this:
@
method( null );
@So I suppose this is not really possible in Qt, correct?
Thanks,
Stephan -
It has nothing to do with Qt, it's just c++ and yes, it's quite possible. Just use a pointer instead of reference:
@
void method( QString* val = nullptr ) {
if ( val != nullptr ) {
// do some processing
*val = ...;
}
}//then call it:
QString s;
method(); //does nothing
method(nullptr); //does nothing explicitly
method(&s); // puts stuff into s
@ -
But to be honest this design is not very good because the responsibility separation is poor.
Instead of this
@void method(Type* param) {
//do some stuff
if(param)
*param = //put stuff into param
}//call
Type p;
method();
method(&p);
@
I would go with this:
@
void doStuff() {
//do stuff
}
Type getSomeStuff() {
//call doStuff() here if it's always required
return //some stuff
}//call
doStuff();
Type p = getSomeStuff();
@
The responsibility division is much cleaner. It's easier to call just the "doStuff" part without worying about what side effects a call could have and (on the low level side) you're avoiding the check entirely. -
Thanks for the initiative, I have changed it and use a mixture of overloading and your suggestion, looks quite good. :o)
CU,
Stephan