r/cpp_questions • u/hyperchompgames • 21h ago
SOLVED When to use copy/move constructors?
SOLVED: There's already been a lot of good answers. I understand much better now. The video here and the reply here were especially helpful. Thanks everyone, great community I did not expect to get such good answers so quickly :)
I'm sort of new to C++ dev, but not at all new to programming. I have some experience in C and old style C++ (ie writing C++ as though it's C). But now I'm trying to properly learn more modern C++, I'm using C++ 20 at the moment.
I've spent a lot of time recently learning about lvalues and rvalues, copy and move semantics. This has been rough to grasp vs C where everything is by value and the only thing you really think about is whether you pass with a pointer or not.
At this point I think I understand how it works. I know how to implement copy constructors, move constructors, and how to use smart pointers like unique_ptr and shared_ptr. I also learned how to create custom deleters for wrapping C libraries so I can use smart pointers with them if I choose to.
The issue now is I feel like there are so many options it's hard to think of when you need each thing. Specifically I don't really understand the need for all this when you can use a smart pointer like unique_ptr, and my understanding is if you return a unique_ptr from a function it is already moved by default? Is that correct?
So when do you actually need a class to overload the move constructor? This is what I don't understand. And why not just pass by lvalue &reference instead? I know smart pointers don't have that option, but that's what std::move is for, right?
I guess it's like at this point I get the rvalue is a literal, temporary value, but I don't really understand what the benefit of passing in this manner is as opposed to passing by lvalue reference? And I don't understand WHEN to do it, because all the information I find focuses solely on the how but not the why.
EDIT: fixing code snippet formatting
View all comments
2
u/IyeOnline 20h ago
Guess how smart pointers are implemented? They are class types with move (and potentially copy) constructors that do things.
Similarly
std::stringis just a class that has a copy constructor (that creates a new copy of the text) and a move constructor (that takes the source objects owned char array).Yes. Nobably a
unique_ptrcannot be copied. It has unique ownership over its pointee. When returning a value from a function, C++ will either elide the return object entirely (RVO/NRVO) or at least return by implicit move.You need to do that in the same situations where you would implement a custom destructor or copy constructor: Your class owns some resource that needs to be properly handled. In the case of move, there is two scenarions:
As a practical example, consider
Sure, you could read in the line, then copy that line variable into the vector. But you dont need this copy of the line anymore: Thus its better to just move it and avoid the copy.