mardi 4 août 2015

C++11 Get non-default constructed object as output parameter

I have a custom method that pops an object from a queue of value objects as out parameter and returns an error code:

class Element
{
public:
    Element() = delete;
    Element(int32_t a, constant std::string &s);

private:
    int32_t a_;
    std::string &s_;
}

class QueueWrapper
{
public:
    ErrorCode push(const Element &e);
    ErrorCode pop(Element &outE); // Somewhere inside: e = queue_.pop();

private 
    std::queue<Element> queue_;
}

void function()
{
    QueueWrapper queueWrapper;

    Element e1(1, "1");
    queueWrapper.push(e1);

    // What should I do here?
    // Element e2;
    // queueWrapper.pop(e2);
}

Can I get a non-default constructed object as output parameter using move semantics or other mechanisms?

Aucun commentaire:

Enregistrer un commentaire