vendredi 1 septembre 2017

Is it possible to keep a reference to an object after it was moved

If I have an object that uses move-semantics to get passed around, is it possible to keep a reference/pointer to that object? Here is a very stripped down example to demonstrate what I mean.

class A
{
    public:
        int v;
};

class B
{
public:
    int *c;

    A foo(void)
    {
        A a;
        // Can I change this somehow so it works even after 'a' goes out of scope?
        // (Without making A::v dynamically allocated)
        this->c = &a.v; 
        return a;
    }
};

Aucun commentaire:

Enregistrer un commentaire