Passing optional parameter by reference in c++

后端 未结 10 1832
天涯浪人
天涯浪人 2020-12-02 22:39

I\'m having a problem with optional function parameter in C++

What I\'m trying to do is to write function with optional parameter which is passed by reference, so th

10条回答
  •  遥遥无期
    2020-12-02 23:23

    This is how I solved this question:

    My original function didn't have a returned error string: bool MyClass::validateXML(const QString& fileName, const QUri& schemaUri);

    I wanted to add the results of the validation in an error string so I implemented: bool MyClass::validateXML(const QString& fileName, const QUri& schemaUri, QString& errorString = *(std::make_unique().get()));

    This way, you can reference the errorString in validateXML without checking if it's valid, and no memory leaks.

提交回复
热议问题