CILGINIMAM
CILGINIMAM

Reputation: 113

How can i return unique_ptr with pointer function?

i don't understand fully this problem. This is my function and i need return class with unique_ptr how can i this?

my list:

std::list<std::unique_ptr<Maths> > l_MathsList;

my function

Maths* Maths2::FindMathsID(int iID)
{
    auto mpClass= std::unique_ptr<Maths>();
    for (auto&& it : l_MathsList)
    {
        if (iter->IsMy(iID)) { 
            mpClass = std::move(it); 
            }
            break; 
        }
    }
    return mpClass; // compiler error

Upvotes: 1

Views: 87

Answers (1)

Hatted Rooster
Hatted Rooster

Reputation: 36513

You probably just want to return a non-owning pointer to the collection element right?

In that case just use .get():

Maths* Maths2::FindMathsID(int iID)
{
    for (auto& it : l_MathsList)
    {
        if (it->IsMy(iID))
          return it.get();
    }
    return nullptr;
}

Upvotes: 1

Related Questions