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
3
u/FrostshockFTW 20h ago
Most classes you create should define no destructor, copy/move constructor, or copy/move assignment (rule of 0). They will inherit the correct behaviour from their members (eg. if your class manages a resource, but it's in a
unique_ptrmember, you will get the correct set of implicitly defined functions for free).https://en.cppreference.com/cpp/language/rule_of_three
2
u/Hkiggity 21h ago
https://youtu.be/F-7Rpt2D-zo?is=h8Y1uKNH8g6d2QM3
I remember watching this a while ago. And i think it’s a good explanation
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.
5
u/Ok_Beginning_9943 21h ago
A copy constructor should create copies of resources , and that's expensive.
A move constructor is transferring ownership of resources currently owned by another object. This avoids the copy, so it's cheaper (almost no-cost).
So the key question is: do u want a copy, or do u want to continue using the same resources you already paid to create?
Does that make sense?
1
u/Kadabrium 21h ago
C creates shallow copy
Shallow copy creates double free
Double free created deep copy
Deep copy creates slowness
Slowness brings you back to shallow copy... but this time marking the original as empty so you dont double free anymore