vendredi 26 février 2016

C++11 rvalue references vs references vs pointers vs "normal" members

I have code like this:

class A{
};

class B{
   B(A &&a) : a(std::move(a)){};
   A a;
}

A a{};
B b{ std::move(a) };
// a is unusable

I use rvalue, because I do not want object to be copy-ed.

I am thinking of changing the class B to use references:

class A{
};

class B{
   B(A &a) : a(a){};
   A &a;
}

A a{};
B b{ a };
// a is usable

Will there be any negative effects of this, except live-time concerns? I do not reassign the a object, so I do not need pointers. but should I consider them?

Aucun commentaire:

Enregistrer un commentaire