简体   繁体   中英

Conversion from QString* to QString&

I'm confused about using pointers and references and I'm facing a little problem.

I got a function :

bool myObject::isFlag( QString &pArgument) const { }

And I'm using it with :

QStringList::const_iterator myQStringList.begin();

[...] && !isFlag( QString( *(myVar + 1)))

I got an error for the

QString( *(myVar + 1)))

which specifies taht no matching function is found.

However I'm pretty sure this should be good ... do you happen to know what could be the problem ?

In this call

isFlag( QString( *(myVar + 1)))

the argument is a temporary QString . You cannot bind non-const references to temporaries, you you would need to change isFlag to take a const reference:

bool myObject::isFlag( const QString &pArgument) const { 

If you cannot use a const reference, then you should create a QString , then pass it to the function:

QString qs( *(myVar + 1));
isFlag(qs);

All of the above assumes that a QString can be constructed from, *(myvar +1) , which is, again, a temporary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM