I have a method that has this signature
void SetFoo(QString& foo);
and I’m trying to pass an empty string inline, but none of the following compile
SetFoo("");
SetFoo(QString(""));
(error: no matching function for call to ‘MyClass::SetFoo(QString)’)
but if I create a variable like this, it works.
QString emptyFoo = "";
SetFoo(emptyFoo);
Is there not a way to call the method without creating a variable explicitly?
NOTE:
Everything seem to work in windows environment with using vc++ compiler but I encounter the above mentioned compilation error in linux environment using g++.
You can create a global object as a
const QString nullStr()and use it everywhere- Somewhat similar to Null Object Pattern.Alternatively as billz mentions, a
constreference to a temporary can exist, so making itconst Qstring&will enable the first 2 versionsRegardless, you should change the reference to
const Qstring&if you dont intend to modify it.