Reputation: 10539
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?
Upvotes: 0
Views: 166
Reputation: 72356
Why not let the caller decide whether to move or copy its A
object?
class A {};
class B {
public:
B(A a_param) : a(std::move(a_param)) {}
private:
A a;
};
void f1() {
A a;
B b{a}; // Makes a copy, a is still usable.
}
void f2() {
A a;
B b{std::move(a)}; // Does a move, a is not usable.
}
void f3() {
B b{A{}}; // Does a move of the temporary object.
}
Upvotes: 1