K
K
Kalombyr2019-04-29 15:03:33
Qt
Kalombyr, 2019-04-29 15:03:33

Why does it compile and in general how does the place in the code work and what, was it possible (QSharedPointer)?

Good afternoon!
There is a code:

.....
typedef QSharedPointer<QDataStream> AnswerData;
.....
Executor::AnswerData Executor::sendSync(int timeout)
{
    if ( !_serial->isConnected() ) return false;
    .......
}
.......
double Heating::getCurrentTemp()
{
    ......
    Executor::AnswerData data = _executer->sendSync();
    ......
    if (!data)  return -1;
    ......
}

As you can see, the sendSync function should return a QSharedPointer, but in case of an error it returns false,
but in getCurrentTemp the result is checked for false and exits if something happens.
And most importantly, it works (seemingly).
I did not find in the QSharedPointer documentation why it could be done this way? Is it possible at all (will there be any "surprises")?
PS Compiler MinGW, Qt 5.10.0

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2019-04-29
@Kalombyr

It's not really clear why you do this. Try a clean example like:

QSharedPointer<QDataStream> doFoo()
{
    return false;
}

Perhaps you have something wrong with the flags, or the environment, or the compiler, or the version of Qt. The point is that QSharedPointer does not have such a constructor. I'll make an assumption that you have MSVC instead of a compiler :)
I advise you to return some DefaultAnswer or just nullptr.
Roman , those links that you provided are not relevant. They are about type casting to bool, but not vice versa.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question